字典破坏源生成器
我一直在尝试使用源生成器并遇到一个奇怪的问题。
源生成器在对其代码进行更改后仅会成功执行一次,但随后的每次运行都会失败:
CSC : warning CS8032: An instance of analyzer Generator cannot be created from MyDir\CodeGeneration\bin\Debug\netstandard2.0\CodeGeneration.dll : Exception has been thrown by the target of an invocation..
我一一删除了部分代码并跟踪到了这一点:
private Dictionary<string, Func<Table, object>> Properties { get; } = new();
这是我想要的一本字典通过反射填充以生成派生类。 如果我删除字典,问题就会消失。另外,如果我用其他东西替换 Table
。
不过,Table
没有什么特别之处:
public class Table
{
public virtual int Object_ID { get; set; }
public virtual int Schema_ID { get; set; }
public virtual string Name { get; set; } = "";
public Table()
{
}
public Table(int object_ID, int schema_ID, string name)
{
Object_ID = object_ID;
Schema_ID = schema_ID;
Name = name;
}
}
我的猜测是,从源生成器(在本例中是 .net 标准 2.0 类库)引用其他项目会导致问题吗?不过,无法在源代码生成中使用反射似乎是一个很大的限制?
为了完整起见,我用来测试它的生成器:
[Generator]
public class Generator : ISourceGenerator
{
private Dictionary<string, Func<Generator, object>> Properties { get; } = new();
public void Execute(GeneratorExecutionContext context)
{
}
public void Initialize(GeneratorInitializationContext context)
{
#if DEBUG
if (!Debugger.IsAttached)
{
Debugger.Launch();
}
#endif
}
}
I´ve been trying something out with source generators and encountered a weird issue.
The source generator will be executed successfully only one time after making changes to it´s code, but will fail every subsequent run with this:
CSC : warning CS8032: An instance of analyzer Generator cannot be created from MyDir\CodeGeneration\bin\Debug\netstandard2.0\CodeGeneration.dll : Exception has been thrown by the target of an invocation..
I removed parts of the code one by one and tracked it down to this:
private Dictionary<string, Func<Table, object>> Properties { get; } = new();
It´s a dictionary I wanted to populate through reflection to generate derived classes.
If I remove the dicitionary the issue disappears. Also if I replace Table
with something else.
There is nothing special about Table
though:
public class Table
{
public virtual int Object_ID { get; set; }
public virtual int Schema_ID { get; set; }
public virtual string Name { get; set; } = "";
public Table()
{
}
public Table(int object_ID, int schema_ID, string name)
{
Object_ID = object_ID;
Schema_ID = schema_ID;
Name = name;
}
}
My guess is that referencing other projects from a source generator (in this case a .net standard 2.0 class library) is causing issues? Not being able to use reflection in source generation seems like quite the limitation though?
For completeness, the generator I used to test this with:
[Generator]
public class Generator : ISourceGenerator
{
private Dictionary<string, Func<Generator, object>> Properties { get; } = new();
public void Execute(GeneratorExecutionContext context)
{
}
public void Initialize(GeneratorInitializationContext context)
{
#if DEBUG
if (!Debugger.IsAttached)
{
Debugger.Launch();
}
#endif
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
canton7s 到食谱的链接和有关包引用的建议实际上引导我找到了 this 中的解决方案讨论。
在使用生成器的项目中,我需要更改对定义
Table
的类库的现有引用,以包含OutputItemType="Analyzer"
。不过,在我的情况下不需要
ReferenceOutputAssembly="false"
。canton7s link to the cookbook and suggestions about package references actually lead me to the solution in this discussion.
In the project that is consuming the generator, I needed to change the existing reference to the class library that defines
Table
to includeOutputItemType="Analyzer"
.ReferenceOutputAssembly="false"
was not needed in my case though.