首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# NET5.0源生成器基本示例不生成输出

C# NET5.0源生成器基本示例不生成输出
EN

Stack Overflow用户
提问于 2021-03-19 17:59:58
回答 1查看 1.5K关注 0票数 5

我正在尽可能地从Microsoft网站示例复制所有的东西,但是没有任何东西是生成的。我创建了一个MCVE,试图找出为什么会发生以下错误:

代码语言:javascript
复制
Warning CS8034  
Unable to load Analyzer assembly C:\Users\me\source\repos\SourceGenTest\SourceGenerators\bin\Debug\netstandard2.0\SourceGenerators.dll: 
Could not find file 'C:\Users\me\source\repos\SourceGenTest\SourceGenerators\bin\Debug\netstandard2.0\SourceGenerators.dll'.
SourceGenLibrary        1   Active

我已经检查了提供的路径,并且SourceGenerators.dll存在。这个错误对我来说毫无意义。

注意:我提供的MCVE中的,显然没有构建警告。不知道为什么,但仍然没有来自源生成器的输出。

我的目标是拥有这样的工作流:

代码语言:javascript
复制
SourceGenerators (class library)
    is consumed by
        SourceGenLibrary (class library)
            is consumed by
                SourceGenConsole (Console project)

因此,我希望分析器在类库中生成定义,这些定义将在许多项目之间共享。上面的工作流将其简化为一个依赖于一个库的项目,该库依赖于源生成器,以尽可能保持MCVE。

因为复制所有这些东西很烦人,所以如果你懒惰的话,这里有一个上传链接:下载链接MCVE (据推测在发布一周后到期,但源代码在下面)。

相关信息:

代码语言:javascript
复制
$ dotnet --version
5.0.201

$ dotnet --list-sdks
5.0.103 [C:\Program Files\dotnet\sdk]
5.0.201 [C:\Program Files\dotnet\sdk]

这些是源文件和它们包含的内容。

/SourceGenTest.sln

代码语言:javascript
复制
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGenLibrary", "SourceGenLibrary\SourceGenLibrary.csproj", "{8702B80A-ACA3-4546-BBE8-A9FEE686A758}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGenerators", "SourceGenerators\SourceGenerators.csproj", "{D693D3B2-F981-4D6D-AC97-B8C636E8C5C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGenConsole", "SourceGenConsole\SourceGenConsole.csproj", "{3730848C-847C-4DF9-A0B3-5ADB5FE9D1E0}"
EndProject
Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Any CPU = Debug|Any CPU
        Release|Any CPU = Release|Any CPU
    EndGlobalSection
    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {8702B80A-ACA3-4546-BBE8-A9FEE686A758}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {8702B80A-ACA3-4546-BBE8-A9FEE686A758}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {8702B80A-ACA3-4546-BBE8-A9FEE686A758}.Release|Any CPU.ActiveCfg = Release|Any CPU
        {8702B80A-ACA3-4546-BBE8-A9FEE686A758}.Release|Any CPU.Build.0 = Release|Any CPU
        {D693D3B2-F981-4D6D-AC97-B8C636E8C5C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {D693D3B2-F981-4D6D-AC97-B8C636E8C5C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {D693D3B2-F981-4D6D-AC97-B8C636E8C5C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
        {D693D3B2-F981-4D6D-AC97-B8C636E8C5C0}.Release|Any CPU.Build.0 = Release|Any CPU
        {3730848C-847C-4DF9-A0B3-5ADB5FE9D1E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {3730848C-847C-4DF9-A0B3-5ADB5FE9D1E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {3730848C-847C-4DF9-A0B3-5ADB5FE9D1E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
        {3730848C-847C-4DF9-A0B3-5ADB5FE9D1E0}.Release|Any CPU.Build.0 = Release|Any CPU
    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
    GlobalSection(ExtensibilityGlobals) = postSolution
        SolutionGuid = {A3752997-47E1-4086-87C8-1583B5E0EDF1}
    EndGlobalSection
EndGlobal

/SourceGenerators/SourceGenerators.csproj

代码语言:javascript
复制
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" />
  </ItemGroup>

</Project>

/SourceGenerators/Class1.cs

代码语言:javascript
复制
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace SourceGeneratorSamples
{
    [Generator]
    public class HelloWorldGenerator : ISourceGenerator
    {
        public void Execute(GeneratorExecutionContext context)
        {
            // begin creating the source we'll inject into the users compilation
            var sourceBuilder = new StringBuilder(@"
            using System;
            namespace HelloWorldGenerated
            {
                public static class HelloWorld
                {
                    public static void SayHello() 
                    {
                        Console.WriteLine(""Hello from generated code!"");
                        Console.WriteLine(""The following syntax trees existed in the compilation that created this program:"");
            ");

            // using the context, get a list of syntax trees in the users compilation
            var syntaxTrees = context.Compilation.SyntaxTrees;

            // add the filepath of each tree to the class we're building
            foreach (SyntaxTree tree in syntaxTrees)
            {
                sourceBuilder.AppendLine($@"Console.WriteLine(@"" - {tree.FilePath}"");");
            }

            // finish creating the source to inject
            sourceBuilder.Append(@"
                    }
                }
            }");

            // inject the created source into the users compilation
            context.AddSource("helloWorldGenerator", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
        }

        public void Initialize(GeneratorInitializationContext context)
        {
        }
    }
}

/SourceGenLibrary/SourceGenLibrary.csproj

代码语言:javascript
复制
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <LangVersion>preview</LangVersion>
    <AssemblyName>SourceGenLibrary</AssemblyName>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\SourceGenerators\SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>

</Project>

/SourceGenLibrary/Class1.cs

代码语言:javascript
复制
using System;

namespace SourceGenTest
{
    public class Class1
    {
        public static void Func()
        {
            //HelloWorldGenerator.HelloWorld.SayHello();
        }
    }
}

/SourceGenConsole/SourceGenConsole.csproj

代码语言:javascript
复制
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\SourceGenLibrary\SourceGenLibrary.csproj" />
  </ItemGroup>

</Project>

/SourceGenConsole/Program.cs

代码语言:javascript
复制
using System;

namespace SourceGenConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-05-20 00:46:45

对不起,之前的回答完全错了。此外,我的代表太低,不能发表评论,所以我提交这个。

我试图对3个项目做一些类似的事情,并得到一个类似的错误(尽管我还有很多其他问题)。我必须在底层项目中添加对源生成器的引用(在您的例子中是控制台)。此外,我还添加了一个Debugger.Launch(),它有助于深入研究具体问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66713408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档