far*_*121 7 .net c# code-generation c#-9.0
我正在尝试构建一个源代码生成器。现在,只是返回“Hello World”的最基本的静态方法。
生成器项目构建,但生成的代码不可用,调试器从不启动,构建输出显示
CSC:警告 CS8032:无法从 ...\bin\Debug\net5.0\Generator.StaticPropertyEnum.dll 创建分析器 Generator.StaticPropertyEnum.helloWorld 的实例:无法加载文件或程序集“System.Runtime, Version=5.0” .0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。该系统找不到指定的文件..
Microsoft.CodeAnalysis.CSharp和Microsoft.CodeAnalysis.AnalyzersMicrosoft.Net.Compilers.ToolsetVisual Studio:版本 16.8.3
.NET SDK:5.0.101
生成器.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>
Run Code Online (Sandbox Code Playgroud)
测试 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>
Run Code Online (Sandbox Code Playgroud)
发电机
[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
}
}
Run Code Online (Sandbox Code Playgroud)
JL0*_*0PD 10
我有针对可空性支持的源生成netstandard2.0器net5.0。
<TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
<Nullable>enable</Nullable>
Run Code Online (Sandbox Code Playgroud)
以及针对相同框架的示例库。
在 Visual Studio 中构建项目时它会崩溃,但从终端构建正常。
为了解决这个问题,我在将其引用为生成器时更改了目标框架SetTagetFramework,现在它编译时没有任何警告或错误。
<ItemGroup>
<ProjectReference Include="..\MyGenerator\MyGenerator.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="false"
SetTargetFramework="TargetFramework=netstandard2.0" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
源生成器必须是 .Net Standard 2.0 才能在 Visual Studio 中运行。
正如 @Yair-Halberstadt 在另一个答案中提到的,列出的具体错误是由于当前源生成器项目需要针对 netstandard2.0。但是,如果您使用不同的程序集(例如)收到 CS8032 错误,Microsoft.CodeAnalysis, Version=3.0.x ...则问题可能是由 SDK 版本不匹配引起的。例如,在我的计算机上,我有 SDK 5.0.302,其版本为 3.10.0 Microsoft.CodeAnalysis和Microsoft.CodeAnalysis.CSharp。虽然 Generator 项目使用 nuget 获取这些包,但引用生成器的项目构建会从 SDK 解析这些文件。这就是为什么恢复到 3.8.0 对一些评论者来说是有效的,他们安装的 SDK 版本包含这些引用的 3.8.0。
| 归档时间: |
|
| 查看次数: |
3269 次 |
| 最近记录: |