roslyn: MetadataImportOptions.All doesn't work in source generators?
I’m trying to get private members from INamedTypeSymbol using MetadataImportOptions.All. When looking at the generated code in Visual Studio it looks as expected (the generator finds a private field), but when running the app, the private field is not found.
Am I using this option incorrectly? Or what can explain the difference in behavior between the code shown in VS and the actual code that gets executed?
The generator code looks like this:
using System.Linq;
using Microsoft.CodeAnalysis;
namespace MyGenerator
{
[Generator]
public class Generator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var memberNamesProvider = context.CompilationProvider
.Select(static (c, _) =>
{
return c
.WithOptions(c.Options.WithMetadataImportOptions(MetadataImportOptions.All))
.GetTypeByMetadataName("MyLibrary.MyLibraryClass")!.GetMembers().Select(x => x.Name);
});
context.RegisterSourceOutput(memberNamesProvider, (productionContext, memberNames) =>
{
var text =
$@"public class GeneratedResult
{{
public static void Print()
{{
System.Console.WriteLine(""{string.Join(" ", memberNames)}"");
}}
}}";
productionContext.AddSource("GeneratedResult.g.cs", text);
});
}
}
}
Version Used: Visual Studio 17.9.2
Steps to Reproduce:
- Open
GeneratorTest.slnfrom https://github.com/dennis-yemelyanov/MetadataImportOptions/tree/main in Visual Studio. - Check the generated code under the
GenertorTestproject analyzers. It contains PrivateField (as expected): - Run the
GeneratorTestproject. The output does not contain PrivateField, and just prints this:
PublicField .ctor
Expected Behavior:
The app output includes PrivateField.
Actual Behavior:
The app output does not include PrivateField.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (8 by maintainers)
yeah I get that. And I think it’s great that there is a framework like incremental generators that makes it easy to do things in a performant way and tries to make it harder to do something in a non-performant way. I also believe there are 2 categories of scenarios:
For the first category performance is paramount and following best practices is a must, since the project size where the generator will be used and other parameters are unknown. For the second category though I think there is a tradeoff between performance and convenience, since all the parameters such as project size, acceptable build time etc. are well known, and solving a real problem is often more important than achieving perfect numbers.
So far I’ve been in the second category for quite some time, and am very happy with the productivity boost I can achieve using generators. I think it’s one of the best features in .NET, even though I never wrote a generator for public consumption. So I hope that at least none of the existing generator features will be be taken out, just because they don’t meet the bar for the first category of scenarios
You’re using symbols in your pipeline . So you will never have anything incremental here 😃. And those steps will not be cheap, especially the recursive walking.