Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在visual studio中运行C#源代码生成器?_C#_.net_Visual Studio - Fatal编程技术网

如何在visual studio中运行C#源代码生成器?

如何在visual studio中运行C#源代码生成器?,c#,.net,visual-studio,C#,.net,Visual Studio,最近我更新了我自己来学习这项新技术,但是我无法让源代码生成器运行,即使我遵循了一步一步的教程 我所做的: 使用控制台应用程序创建solusion,然后添加类库项目。这两个项目都是.NET5 为这两个项目安装NuGet软件包Microsoft.CodeAnalysis.CSharp.workspace 在类库项目中,编写以下代码: using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Text; using System.Text

最近我更新了我自己来学习这项新技术,但是我无法让源代码生成器运行,即使我遵循了一步一步的教程

我所做的:

  • 使用控制台应用程序创建solusion,然后添加类库项目。这两个项目都是.NET5

  • 为这两个项目安装NuGet软件包
    Microsoft.CodeAnalysis.CSharp.workspace

  • 在类库项目中,编写以下代码:

    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.Text;
    using System.Text;
    
    namespace ClassLibrary {
        [Generator]
        public class MySourceGenerator : ISourceGenerator {
            public void Execute(GeneratorExecutionContext context) {
                context.AddSource("myGeneratedFile.cs", SourceText.From(@"
    namespace GeneratedNamespace
    {
        public class GeneratedClass
        {
            public static void GeneratedMethod()
            {
                // generated code
            }
        }
    }", Encoding.UTF8));
            }
    
            public void Initialize(GeneratorInitializationContext context) {
            }
        }
    }
    
                GeneratedNamespace.GeneratedClass.GeneratedMethod();
    
      <ItemGroup>
        <ProjectReference Include="..\ClassLibrary_ExperimentalSourceGenerator\ClassLibrary_ExperimentalSourceGenerator.csproj"
                          OutputItemType="Analyzer"
                          ReferenceOutputAssembly="false" />
      </ItemGroup>
    
  • 在控制台应用程序的主要方法中,编写以下代码:

    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.Text;
    using System.Text;
    
    namespace ClassLibrary {
        [Generator]
        public class MySourceGenerator : ISourceGenerator {
            public void Execute(GeneratorExecutionContext context) {
                context.AddSource("myGeneratedFile.cs", SourceText.From(@"
    namespace GeneratedNamespace
    {
        public class GeneratedClass
        {
            public static void GeneratedMethod()
            {
                // generated code
            }
        }
    }", Encoding.UTF8));
            }
    
            public void Initialize(GeneratorInitializationContext context) {
            }
        }
    }
    
                GeneratedNamespace.GeneratedClass.GeneratedMethod();
    
      <ItemGroup>
        <ProjectReference Include="..\ClassLibrary_ExperimentalSourceGenerator\ClassLibrary_ExperimentalSourceGenerator.csproj"
                          OutputItemType="Analyzer"
                          ReferenceOutputAssembly="false" />
      </ItemGroup>
    
  • 在console应用程序的.csproj中,添加以下代码:

    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.Text;
    using System.Text;
    
    namespace ClassLibrary {
        [Generator]
        public class MySourceGenerator : ISourceGenerator {
            public void Execute(GeneratorExecutionContext context) {
                context.AddSource("myGeneratedFile.cs", SourceText.From(@"
    namespace GeneratedNamespace
    {
        public class GeneratedClass
        {
            public static void GeneratedMethod()
            {
                // generated code
            }
        }
    }", Encoding.UTF8));
            }
    
            public void Initialize(GeneratorInitializationContext context) {
            }
        }
    }
    
                GeneratedNamespace.GeneratedClass.GeneratedMethod();
    
      <ItemGroup>
        <ProjectReference Include="..\ClassLibrary_ExperimentalSourceGenerator\ClassLibrary_ExperimentalSourceGenerator.csproj"
                          OutputItemType="Analyzer"
                          ReferenceOutputAssembly="false" />
      </ItemGroup>
    
    
    
  • 构建解决方案

  • 收到以下错误:

    CS0103
    当前上下文中不存在名称“GeneratedNamespace” ConsoleApp1 D:\OneDrive\VS Solutions\ConsoleApp1\Program.cs

    及以下警告:

    CS8032
    无法从D:\OneDrive\VS Solutions\ClassLibrary\U ExperimentalSourceGenerator\bin\Debug\net5.0\ClassLibrary\U ExperimentalSourceGenerator.dll创建analyzer ClassLibrary.MySourceGenerator的实例:无法加载文件或程序集'System.Runtime,版本=5.0.0.0,区域性=中性,PublicKeyToken=b03f5f7f11d50a3a'或其依赖项之一。系统找不到指定的文件


  • 我相信这里的问题是源代码生成器程序集的目标是.NET5

    源生成器必须以netstandard2.0为目标,才能在Visual Studio中正常工作。这是因为VS在内部运行
    net48
    ,因此无法加载针对较新框架的代码

    此处的提示是错误消息:

    无法加载文件或程序集“System.Runtime,Version=5.0.0.0…”