zoukankan      html  css  js  c++  java
  • C# ISourceGenerator

    通过ISourceGenerator可以实现代码生成,这个例子网上好多,就不再重复说了,但是有几个关键点不注意的话,生成不了代码

    项目结构如下

    1、dll代码

    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.Text;
    using System.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)
            {
                // No initialization required for this one
            }
        }
    }
    

     2、调用代码

    using Nest;
    using System;
    
    using SourceGeneratorSamples;
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                SomeMethodIHave();
    
    
            }
            static void SomeMethodIHave()
            {
                HelloWorldGenerated.HelloWorld.SayHello(); // calls Console.WriteLine("Hello World!") and then prints out 		syntax trees
            }
        }
    }
    

    3、需要注意两点

    调用者proj文件里要这样写

      <ItemGroup>
        <ProjectReference Include="..ClassLibrary2ClassLibrary2.csproj">
          <Private>true</Private>
        </ProjectReference>
      </ItemGroup>
    
      <ItemGroup>
          <Analyzer Include="..ClassLibrary2inDebug
    etstandard2.0ClassLibrary2.dll"/>
      </ItemGroup>
    

    或者这样写

     <ItemGroup>
        <ProjectReference Include="..ClassLibrary2ClassLibrary2.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true"/>
     </ItemGroup>
    

      

      

      

  • 相关阅读:
    升级xcode7.0 第三方库不能用的解决方法(bitcode是什么鬼?)
    java调用C#的dll
    微信JS SDK Demo
    【jQuery】smartMenu右键自定义上下文菜单插件(似web QQ)
    搭建Windows SVN服务器及TortoiseSVN使用帮助和下载
    微信jsSDK开发
    用c#开发微信(10) JSSDK 基本用法 分享接口“发送到朋友”
    MVC4.0网站发布和部署到IIS7.0上的方法
    C#的事件处理机制
    ASP.Net MVC开发基础学习笔记(1):走向MVC模式
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/14065769.html
Copyright © 2011-2022 走看看