Getting started
editInstall the package
Add the package to an analyzer, code fix or source generator project that already references the Roslyn packages, with PrivateAssets="all":
<ItemGroup>
<PackageReference Include="Meziantou.Framework.Roslyn" Version="x.y.z" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.6.0" PrivateAssets="all" />
</ItemGroup>The package contains no assembly: its source files are compiled into your project. They compile with Microsoft.CodeAnalysis 4.8 and later, and use the APIs of newer versions when they are available, thanks to compilation constants.
Import the namespace
The helpers are extension methods and types of the Meziantou.Framework.Roslyn namespace:
using Meziantou.Framework.Roslyn;Use the helpers
The types are internal: they belong to your assembly, and do not conflict with the copies compiled into other assemblies. A test project that sees your internals through [InternalsVisibleTo] and also uses the package does not get CS0436 warnings either: see Build configuration.
var compilation = CSharpCompilation.Create(
"Sample",
[CSharpSyntaxTree.ParseText("public class Base { } public class Derived : Base { }")],
[MetadataReference.CreateFromFile(typeof(object).Assembly.Location)]);
var baseType = compilation.GetTypeByMetadataName("Base")!;
var type = compilation.GetTypeByMetadataName("Derived")!;
// true when the type is Base or derives from it
Assert.True(type.IsOrInheritsFrom(baseType));
Assert.True(baseType.IsOrInheritsFrom(baseType));
Assert.False(baseType.IsOrInheritsFrom(type));Next steps
- Symbols and types: type relationships and attributes.
- Reporting diagnostics: the
ReportDiagnosticextension methods.