【发布时间】:2021-09-04 08:59:39
【问题描述】:
我想生成一个静态类,该类应该有一个方法,具体取决于特定参考程序集中的其他类。
一个简化的例子:
// Generator.csproj
[Generator]
public class MyGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
// Register a factory that can create our custom syntax receiver
context.RegisterForSyntaxNotifications(() => new MySyntaxReceiver());
}
public void Execute(GeneratorExecutionContext context)
{
// var syntaxReceiver = (MySyntaxReceiver)context.SyntaxReceiver;
}
}
private class MySyntaxReceiver : ISyntaxReceiver
{
....
}
// Core.csproj
// namespace Core.Entities
class Entity1 : IAccessControl {}
class Entity2 {}
class Entity3 : IAccessControl {}
// Persistence.csproj => has a reference to Core project and the Generator
// this class should be generated ...
static class GeneratedClass
{
public static void DoSomethingEntity1()
public static void DoSomethingEntity3()
}
我想在Core项目中找到Entity类,并在Persistence项目中生成一个类,
问题是我的Core 项目无法访问,它已经在Persistence 之前编译。我应该使用反射还是手动读取核心实体?或者有没有更好的方法来访问 Core 项目中的 SyntaxTree?
【问题讨论】:
标签: c# sourcegenerators csharp-source-generator