首页 > 其他分享 >Source Generator-编译本地文件的中的代码

Source Generator-编译本地文件的中的代码

时间:2023-03-20 16:11:44浏览次数:39  
标签:xml 文件 Generator 编译 Source context using

上一篇讲解了如何直接调用静态方法,而有时候我们会生成cs文件或其他格式的文件,进而使用Source Generator编译。本例中就对资源文件进行编译进行举例说明。

  1. 在Source Generator调用的基础上,创建一个新的类库项目(SourceGeneratorXmlMethod)

  2. 在项目上添加引用

      <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" PrivateAssets="all" />
        <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
      </ItemGroup>
    
  3. 新增cs文件(SourceGeneratorXmlGenerator),添加如下代码

    using Microsoft.CodeAnalysis.Text;
    using Microsoft.CodeAnalysis;
    using System;
    using System.Text;
    using System.Linq;
    using System.Diagnostics;
    
    namespace SourceGeneratorXmlMethod
    {
        [Generator]
        public class SourceGeneratorXmlGenerator : ISourceGenerator
        {
            public void Initialize(GeneratorInitializationContext context) { }
    
            public void Execute(GeneratorExecutionContext context)
            {
                // 编译的时候弹出调试工具
                Debugger.Launch();
                // 获得查找格式为xml的文件(控制台命令的文件列表)
                var myFiles = context.AdditionalFiles.Where(at => at.Path.EndsWith(".xml"));
                foreach (var file in myFiles)
                {
                    // 获得xml的文件信息
                    var content = file.GetText(context.CancellationToken);
                    // 获得xml中的信息
                    String codes = content.ToString();
                    // 主要为演示,只是去掉了node标签
                    codes = codes.Replace("<root>", "").Replace("</root>", "");
                    // 动态编译
                    context.AddSource($"generated.cs", SourceText.From(codes, Encoding.UTF8));
                }
            }
        }
    }
    
  4. 在SourceGeneratorConsole项目中添加file.xml文件,并添加如下信息

    <root>
      namespace XmlMethodNamespace
      {
        public class XmlMethodClass
        {
          public static void StaticXmlMethod()
          {
            Console.WriteLine("Hellow Xml");
          }
        }
      }
    </root>
    

    root标签中为c#代码

  5. 双击SourceGeneratorConsole项目,修改file.xml的引用方式,改为

      <ItemGroup>
        <AdditionalFiles Include="file.xml" CopyToOutputDirectory="PreserveNewest" />
      </ItemGroup>
    
  6. 运行,会出现预期的结果:Hellow Xml

  • 关于资源文件

    • 资源文件的声明是在主项目中,而非实现ISourceGenerator接口的项目中。
    • 如果启用了调试,请务必先将要使用的资源文件放入输出目录,否则context.AdditionalFiles中的文件一直是0。因为Debugger.Launch()是编译时调试,文件还未复制到输出目录中。
  • 关于调试

    1. 可在ISourceGenerator接口实现代码中添加Debugger.Launch()方法,当编译或运行前会进入到弹出实时编译的选择

    2. 点击确定后就可对要生成的代码进行编译

标签:xml,文件,Generator,编译,Source,context,using
From: https://www.cnblogs.com/wanghun315/p/17236667.html

相关文章

  • resources目录下的mapper写sql语句没有提示
    resources目录下的mapper写sql语句没有提示首先了解一下mybatix-config.xml连接mapper文件的三种方式:<!--第一种--><mapperresource="com/bbl/dao/UserMa......
  • Source Insight 4.0使用介绍
    简介SourceInsight是一个功能非常强大的,面向项目的程序编辑器,具有针对C/C++,C#,Java,Objective-C等的内置动态分析。SourceInsight可实现多文件代码中变量和函数的快......
  • aspnetcore mvc 5.0 cshtml修改后进行 实时编译razor
    ASP.NETCore中的Razor文件编译 Razor使用SDK在生成和发布时Razor编译扩展名.cshtml为的文件。可以选择通过配置项目来启用运行时编译。1、安装Microsoft.Asp......
  • golang grpc编译工具的安装
    本次安装前提是已经安装go环境,我的环境是ubuntu20.04。安装环境主要是:protocprotoc-gen-goprotoc-gen-go-grpc1.编译器:protoc的安装参考官方安装方式,方式一如果......
  • 1随堂练习【编译原理】
    课堂测试【编译原理】​​前言​​​​推荐​​​​课堂测试​​​​课前测03-01​​​​课后练习03-01​​​​课后复习03-08​​​​课前热身03-13​​​​最后​​前言20......
  • Vue插件:Vue-resource github搜索示例
     1:安装插件  vue-resourcevue的插件库,在vue1.0年代使用几率很高......
  • 嵌入式开发中常用的条件编译
    条件编译命令常见形式:1#ifdef标识符2程序段13#else4程序段25#endif作用:当标识符被#define定义过,则对程序段1进行编译,否则编译程序段2。常用形式2:#ifndef......
  • docker 容器内 安装nginx ./configure 编译出错问题
    ./configure:error:Ccompilerccisnotfound解决办法aptinstallg++./configure:error:theHTTPrewritemodulerequiresthePCRElibrary.解决办法......
  • 实验一 密码引擎-1-OpenEuler-OpenSSL编译
    任务详情安装Ubuntu和OpenEuler虚拟机下载最新的OpenSSL源码(1.1版本)用自己的8位学号建立一个文件夹,cd你的学号,用pwd获得绝对路径参考https://www.cnblogs.com/roc......
  • 交叉编译
    一、交叉编译概念 交叉编译链1.安装官网https://github.com/raspberrypi/tools   2unziptools-master.zip,解压文件,然后进入文件夹进入文件夹就可以看下是......