I've tried following multiple examples (like Shawn Wildermuth's youtube video), but I simply cannot get the most basic IncrementalGenerator to work.
I have two projects, a console project
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
</Project>
And a generator project
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
</ItemGroup>
</Project>
The generator project has a single generator class
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
namespace ClassLibrary1;
[Generator]
public class Class1 : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var provider = context.SyntaxProvider.CreateSyntaxProvider(
static (syntaxNode, _) => syntaxNode is ClassDeclarationSyntax,
static (ctx, _) => (ClassDeclarationSyntax)ctx.Node)
.Where(x => x != null);
var compilation = context.CompilationProvider
.Combine(provider.Collect());
context.RegisterSourceOutput(compilation, Execute);
}
private void Execute(SourceProductionContext arg1, (Compilation Left, ImmutableArray<ClassDeclarationSyntax> Right) arg2)
{
var theCode = """
namespace Generated;
public static class GeneratedClass
{
public static string GetCode()
{
return "Hello from the generated code!";
}
}
""";
arg1.AddSource("GeneratedClass.g.cs", theCode);
}
}
The console project will not recognize the generated class if I try to use it and the build fails if I reference it. When I build the solution I get a successful build output of both projects. If I put breakpoints in the generator class, they never get hit. I am using Visual Studio 2022.
What do I need to enable/configure to get this to work?
I've tried following multiple examples (like Shawn Wildermuth's youtube video), but I simply cannot get the most basic IncrementalGenerator to work.
I have two projects, a console project
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
</Project>
And a generator project
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
</ItemGroup>
</Project>
The generator project has a single generator class
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
namespace ClassLibrary1;
[Generator]
public class Class1 : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var provider = context.SyntaxProvider.CreateSyntaxProvider(
static (syntaxNode, _) => syntaxNode is ClassDeclarationSyntax,
static (ctx, _) => (ClassDeclarationSyntax)ctx.Node)
.Where(x => x != null);
var compilation = context.CompilationProvider
.Combine(provider.Collect());
context.RegisterSourceOutput(compilation, Execute);
}
private void Execute(SourceProductionContext arg1, (Compilation Left, ImmutableArray<ClassDeclarationSyntax> Right) arg2)
{
var theCode = """
namespace Generated;
public static class GeneratedClass
{
public static string GetCode()
{
return "Hello from the generated code!";
}
}
""";
arg1.AddSource("GeneratedClass.g.cs", theCode);
}
}
The console project will not recognize the generated class if I try to use it and the build fails if I reference it. When I build the solution I get a successful build output of both projects. If I put breakpoints in the generator class, they never get hit. I am using Visual Studio 2022.
What do I need to enable/configure to get this to work?
Share Improve this question asked yesterday ValyrionValyrion 2,61311 gold badges32 silver badges65 bronze badges 2 |1 Answer
Reset to default 0I updated my VS installation to 17.13.5 and that appears to have fixed the issue.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743778520a4505675.html
Debugger.Break
somewhere early in the generator code, like in Initialize, the JIT dialog box should then ask you which instance of VS (or a new one) you want to use – Simon Mourier Commented yesterday