问题描述
我正在尝试构建一个源代码生成器。现在,只是返回“Hello World”的最基本的静态方法。
生成器项目构建,但生成的代码不可用,调试器从不启动,构建输出显示
CSC:警告 CS8032:无法从 ...\bin\Debug\net5.0\Generator.StaticPropertyEnum.dll 创建分析器 Generator.StaticPropertyEnum.helloWorld 的实例:无法加载文件或程序集“System.Runtime,版本=5.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。系统找不到指定的文件..
我引用的例子
- Roslyn Team Generator Sample Project
- Roslyn Team Generator Cookbook
- Generator.Equals Project
- How To Debug C# 9 Source Generators
我试过了
- 更改生成器和测试项目的 TargetFramework 和 LanguageVersion
- 引用许多版本的分析器库
Microsoft.CodeAnalysis.CSharp
和Microsoft.CodeAnalysis.Analyzers
- 引用
Microsoft.Net.Compilers.Toolset
的显式版本 - 添加对 NetStandard 库的显式引用
- 从头开始使用分析器项目模板
- 正在寻找一个生成器项目模板(但没有找到)
版本
Visual Studio:版本 16.8.3
.NET SDK:5.0.101
代码
Generator.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0-2.final" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0" PrivateAssets="all" />
</ItemGroup>
</Project>
测试 csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Generator.StaticPropertyEnum\Generator.StaticPropertyEnum.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
</Project>
发电机
[Generator]
public class helloWorld : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
context.AddSource("HelloWorld-generated.cs",@"
using System;
namespace HelloWorld
{
public static class Hello
{
public static string SayHello() {
return ""HelloWorld"";
}
}
}");
}
public void Initialize(GeneratorInitializationContext context)
{
#if DEBUG
if(!Debugger.IsAttached) Debugger.Launch();
#endif
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)