Skip to main content
Any Framework

Source helpers for Roslyn analyzers and source generators

Published
Aug 23, 2026
1mo ago
Total Downloads
7.2K
7,214

Install

dotnet add package Meziantou.Framework.Roslyn --version 1.0.4

No framework information available.

README

Meziantou.Framework.Roslyn

Meziantou.Framework.Roslyn provides source helpers for Roslyn analyzers and source generators. The package does not ship a library assembly; helper code is compiled into the consuming project.

Usage

Install the package in an analyzer or source generator project that already references Roslyn packages:

<ItemGroup>
  <PackageReference Include="Meziantou.Framework.Roslyn" Version="x.y.z" PrivateAssets="all" />
  <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.6.0" PrivateAssets="all" />
</ItemGroup>

Then import the helper namespace:

using Meziantou.Framework.Roslyn;

Helpers

  • Compilation.GetBestTypeByMetadataName
  • Compilation.IsNet9OrGreater
  • ContextExtensions.ReportDiagnostic
  • DiagnosticReporter
  • GeneratedCodeExtensions.IsGeneratedCodeFile
  • IOperation.UnwrapImplicitConversions
  • LanguageVersionExtensions
  • LocalDataFlowAnalysis
  • LocationExtensions
  • MethodSymbolExtensions
  • NamespaceSymbolExtensions
  • SuppressorHelpers
  • SymbolAttributeExtensions
  • SyntaxNode.HasAutoGeneratedComment
  • SyntaxTree.IsGeneratedCode
  • ISymbol.GetFirstSourceLocation
  • ISymbol.IsVisibleOutsideOfAssembly
  • ITypeSymbol.GetUnderlyingNullableTypeOrSelf

The package also defines Roslyn and C# feature constants before compilation based on the referenced Microsoft.CodeAnalysis.* package version, such as ROSLYN_4_8_OR_GREATER and CSHARP12_OR_GREATER.

Type embedding

All types are decorated with [Microsoft.CodeAnalysis.Embedded], so the compiler hides them from other assemblies. This means the helpers don't conflict when an assembly that uses this package exposes its internals with [InternalsVisibleTo] to another assembly that also uses it (warning CS0436).

The package declares Microsoft.CodeAnalysis.EmbeddedAttribute as an internal sealed partial class without any attribute on it, so it can be merged with the declarations emitted by other source generators or packages.

Define the MEZIANTOU_FRAMEWORK_ROSLYN_DISABLE_EMBEDDEDATTRIBUTE constant to opt out of both the attribute usages and the attribute declaration:

<PropertyGroup>
  <DefineConstants>$(DefineConstants);MEZIANTOU_FRAMEWORK_ROSLYN_DISABLE_EMBEDDEDATTRIBUTE</DefineConstants>
</PropertyGroup>

If the attribute is already declared in the project in a way that conflicts with this declaration, define MEZIANTOU_FRAMEWORK_ROSLYN_DISABLE_EMBEDDEDATTRIBUTE_DECLARATION instead. The types are still marked as embedded, but the attribute must be provided by the project:

<PropertyGroup>
  <DefineConstants>$(DefineConstants);MEZIANTOU_FRAMEWORK_ROSLYN_DISABLE_EMBEDDEDATTRIBUTE_DECLARATION</DefineConstants>
</PropertyGroup>

Compiler warnings

The helper files suppress all compiler warnings so they don't pollute the consuming project's build. Define the MEZIANTOU_FRAMEWORK_ROSLYN_ENABLE_WARNINGS constant to report them:

<PropertyGroup>
  <DefineConstants>$(DefineConstants);MEZIANTOU_FRAMEWORK_ROSLYN_ENABLE_WARNINGS</DefineConstants>
</PropertyGroup>

Changes from Previous Version

No structural changes between v1.0.3 and v1.0.4.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

No target framework metadata is available. Confirm compatibility in a representative project before standardising on this package.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.