中暖 發表於 2020-4-30 20:19:00

C# 9 新特性:代码生成器、编译时反射

<h2 id="前言">前言</h2>
<p>今天 .NET 官方博客宣布 C# 9 Source Generators 第一个预览版发布,这是一个用户已经喊了快 5 年特性,今天终于发布了。</p>
<h2 id="简介">简介</h2>
<p>Source Generators 顾名思义代码生成器,它允许开发者在代码编译过程中获取查看用户代码并且生成新的 C# 代码参与编译过程,并且可以很好的与代码分析器集成提供 Intellisense、调试信息和报错信息,可以用它来做代码生成,因此也相当于是一个加强版本的编译时反射。</p>
<p>使用 Source Generators,可以做到这些事情:</p>
<ul>
<li>获取一个 Compilation 对象,这个对象表示了所有正在编译的用户代码,你可以从中获取 AST 和语义模型等信息</li>
<li>可以向 Compilation 对象中插入新的代码,让编译器连同已有的用户代码一起编译</li>
</ul>
<p>Source Generators 作为编译过程中的一个阶段执行:</p>
<p>编译运行 -&gt; [分析源代码 -&gt; 生成新代码] -&gt; 将生成的新代码添加入编译过程 -&gt; 编译继续。</p>
<p>上述流程中,中括号包括的内容即为 Source Generators 所参与的阶段和能做到的事情。</p>
<h2 id="作用">作用</h2>
<p>.NET 明明具备运行时反射和动态 IL 织入功能,那这个 Source Generators 有什么用呢?</p>
<h3 id="编译时反射---0-运行时开销">编译时反射 - 0 运行时开销</h3>
<p>拿 ASP.NET Core 举例,启动一个 ASP.NET Core 应用时,首先会通过运行时反射来发现 Controllers、Services 等的类型定义,然后在请求管道中需要通过运行时反射获取其构造函数信息以便于进行依赖注入。然而运行时反射开销很大,即使缓存了类型签名,对于刚刚启动后的应用也无任何帮助作用,而且不利于做 AOT 编译。</p>
<p>Source Generators 将可以让 ASP.NET Core 所有的类型发现、依赖注入等在编译时就全部完成并编译到最终的程序集当中,最终做到 0 运行时反射使用,不仅利于 AOT 编译,而且运行时 0 开销。</p>
<p>除了上述作用之外,gRPC 等也可以利用此功能在编译时织入代码参与编译,不需要再利用任何的 MSBuild Task 做代码生成啦!</p>
<p>另外,甚至还可以读取 XML、JSON 直接生成 C# 代码参与编译,DTO 编写全自动化都是没问题的。</p>
<h3 id="aot-编译">AOT 编译</h3>
<p>Source Generators 的另一个作用是可以帮助消除 AOT 编译优化的主要障碍。</p>
<p>许多框架和库都大量使用反射,例如System.Text.Json、System.Text.RegularExpressions、ASP.NET Core 和 WPF 等等,它们在运行时从用户代码中发现类型。这些非常不利于 AOT 编译优化,因为为了使反射能够正常工作,必须将大量额外甚至可能不需要的类型元数据编译到最终的原生映像当中。</p>
<p>有了 Source Generators 之后,只需要做编译时代码生成便可以避免大部分的运行时反射的使用,让 AOT 编译优化工具能够更好的运行。</p>
<h2 id="例子">例子</h2>
<h3 id="inotifypropertychanged"><code>INotifyPropertyChanged</code></h3>
<p>写过 WPF 或 UWP 的都知道,在 ViewModel 中为了使属性变更可被发现,需要实现 <code>INotifyPropertyChanged</code> 接口,并且在每一个需要的属性的 <code>setter</code> 处触发属性更改事件:</p>
<pre><code class="language-csharp">class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    private string _text;
    public string Text
    {
      get =&gt; _text;
      set
      {
            _text = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Text)));
      }
    }
}
</code></pre>
<p>当属性多了之后将会非常繁琐,先前 C# 引入了 <code>CallerMemberName</code> 用于简化属性较多时候的情况:</p>
<pre><code class="language-csharp">class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    private string _text;
    public string Text
    {
      get =&gt; _text;
      set
      {
            _text = value;
            OnPropertyChanged();
      }
    }

    protected virtual void OnPropertyChanged( string? propertyName = null)
    {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
</code></pre>
<p>即,用 <code>CallerMemberName</code> 指示参数,在编译时自动填充调用方的成员名称。</p>
<p>但是还是不方便。</p>
<p>如今有了 Source Generators,我们可以在编译时生成代码做到这一点了。</p>
<p>为了实现 Source Generators,我们需要写个实现了 <code>ISourceGenerator</code> 并且标注了 <code>Generator</code> 的类型。</p>
<p>完整的 Source Generators 代码如下:</p>
<pre><code class="language-csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

namespace MySourceGenerator
{
   
    public class AutoNotifyGenerator : ISourceGenerator
    {
      private const string attributeText = @"
using System;
namespace AutoNotify
{
   
    sealed class AutoNotifyAttribute : Attribute
    {
      public AutoNotifyAttribute()
      {
      }
      public string PropertyName { get; set; }
    }
}
";

      public void Initialize(InitializationContext context)
      {
            // 注册一个语法接收器,会在每次生成时被创建
            context.RegisterForSyntaxNotifications(() =&gt; new SyntaxReceiver());
      }

      public void Execute(SourceGeneratorContext context)
      {
            // 添加 Attrbite 文本
            context.AddSource("AutoNotifyAttribute", SourceText.From(attributeText, Encoding.UTF8));

            // 获取先前的语法接收器
            if (!(context.SyntaxReceiver is SyntaxReceiver receiver))
                return;

            // 创建处目标名称的属性
            CSharpParseOptions options = (context.Compilation as CSharpCompilation).SyntaxTrees.Options as CSharpParseOptions;
            Compilation compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(attributeText, Encoding.UTF8), options));

            // 获取新绑定的 Attribute,并获取INotifyPropertyChanged
            INamedTypeSymbol attributeSymbol = compilation.GetTypeByMetadataName("AutoNotify.AutoNotifyAttribute");
            INamedTypeSymbol notifySymbol = compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanged");

            // 遍历字段,只保留有 AutoNotify 标注的字段
            List&lt;IFieldSymbol&gt; fieldSymbols = new List&lt;IFieldSymbol&gt;();
            foreach (FieldDeclarationSyntax field in receiver.CandidateFields)
            {
                SemanticModel model = compilation.GetSemanticModel(field.SyntaxTree);
                foreach (VariableDeclaratorSyntax variable in field.Declaration.Variables)
                {
                  // 获取字段符号信息,如果有 AutoNotify 标注则保存
                  IFieldSymbol fieldSymbol = model.GetDeclaredSymbol(variable) as IFieldSymbol;
                  if (fieldSymbol.GetAttributes().Any(ad =&gt; ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default)))
                  {
                        fieldSymbols.Add(fieldSymbol);
                  }
                }
            }

            // 按 class 对字段进行分组,并生成代码
            foreach (IGrouping&lt;INamedTypeSymbol, IFieldSymbol&gt; group in fieldSymbols.GroupBy(f =&gt; f.ContainingType))
            {
                string classSource = ProcessClass(group.Key, group.ToList(), attributeSymbol, notifySymbol, context);
               context.AddSource($"{group.Key.Name}_autoNotify.cs", SourceText.From(classSource, Encoding.UTF8));
            }
      }

      private string ProcessClass(INamedTypeSymbol classSymbol, List&lt;IFieldSymbol&gt; fields, ISymbol attributeSymbol, ISymbol notifySymbol, SourceGeneratorContext context)
      {
            if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default))
            {
                // TODO: 必须在顶层,产生诊断信息
                return null;
            }

            string namespaceName = classSymbol.ContainingNamespace.ToDisplayString();

            // 开始构建要生成的代码
            StringBuilder source = new StringBuilder($@"
namespace {namespaceName}
{{
    public partial class {classSymbol.Name} : {notifySymbol.ToDisplayString()}
    {{
");

            // 如果类型还没有实现 INotifyPropertyChanged 则添加实现
            if (!classSymbol.Interfaces.Contains(notifySymbol))
            {
                source.Append("public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;");
            }

            // 生成属性
            foreach (IFieldSymbol fieldSymbol in fields)
            {
                ProcessField(source, fieldSymbol, attributeSymbol);
            }

            source.Append("} }");
            return source.ToString();
      }

      private void ProcessField(StringBuilder source, IFieldSymbol fieldSymbol, ISymbol attributeSymbol)
      {
            // 获取字段名称
            string fieldName = fieldSymbol.Name;
            ITypeSymbol fieldType = fieldSymbol.Type;

            // 获取 AutoNotify Attribute 和相关的数据
            AttributeData attributeData = fieldSymbol.GetAttributes().Single(ad =&gt; ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default));
            TypedConstant overridenNameOpt = attributeData.NamedArguments.SingleOrDefault(kvp =&gt; kvp.Key == "PropertyName").Value;

            string propertyName = chooseName(fieldName, overridenNameOpt);
            if (propertyName.Length == 0 || propertyName == fieldName)
            {
                //TODO: 无法处理,产生诊断信息
                return;
            }

            source.Append($@"
public {fieldType} {propertyName}
{{
    get
    {{
      return this.{fieldName};
    }}
    set
    {{
      this.{fieldName} = value;
      this.PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof({propertyName})));
    }}
}}
");

            string chooseName(string fieldName, TypedConstant overridenNameOpt)
            {
                if (!overridenNameOpt.IsNull)
                {
                  return overridenNameOpt.Value.ToString();
                }

                fieldName = fieldName.TrimStart('_');
                if (fieldName.Length == 0)
                  return string.Empty;

                if (fieldName.Length == 1)
                  return fieldName.ToUpper();

                return fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1);
            }

      }

      // 语法接收器,将在每次生成代码时被按需创建
      class SyntaxReceiver : ISyntaxReceiver
      {
            public List&lt;FieldDeclarationSyntax&gt; CandidateFields { get; } = new List&lt;FieldDeclarationSyntax&gt;();

            // 编译中在访问每个语法节点时被调用,我们可以检查节点并保存任何对生成有用的信息
            public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
            {
                // 将具有至少一个 Attribute 的任何字段作为候选
                if (syntaxNode is FieldDeclarationSyntax fieldDeclarationSyntax
                  &amp;&amp; fieldDeclarationSyntax.AttributeLists.Count &gt; 0)
                {
                  CandidateFields.Add(fieldDeclarationSyntax);
                }
            }
      }
    }
}
</code></pre>
<p>有了上述代码生成器之后,以后我们只需要这样写 ViewModel 就会自动生成通知接口的事件触发调用:</p>
<pre><code class="language-csharp">public partial class MyViewModel
{
   
    private string _text = "private field text";

   
    private int _amount = 5;
}
</code></pre>
<p>上述代码将会在编译时自动生成以下代码参与编译:</p>
<pre><code class="language-csharp">public partial class MyViewModel : System.ComponentModel.INotifyPropertyChanged
{
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    public string Text
    {
      get
      {
            return this._text;
      }
      set
      {
            this._text = value;
            this.PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Text)));
      }
    }

    public int Count
    {
      get
      {
            return this._amount;
      }
      set
      {
            this._amount = value;
            this.PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(Count)));
      }
    }
}
</code></pre>
<p>非常方便!</p>
<p>使用时,将 Source Generators 部分作为一个独立的 .NET Standard 2.0 程序集(暂时不支持 2.1),用以下方式引入到你的项目即可:</p>
<pre><code class="language-xml">&lt;ItemGroup&gt;
&lt;Analyzer Include="..\MySourceGenerator\bin\$(Configuration)\netstandard2.0\MySourceGenerator.dll" /&gt;
&lt;/ItemGroup&gt;

&lt;ItemGroup&gt;
&lt;ProjectReference Include="..\MySourceGenerator\MySourceGenerator.csproj" /&gt;
&lt;/ItemGroup&gt;
</code></pre>
<p>注意需要最新的 .NET 5 preview(写文章时还在 artifacts 里没正式 release),并指定语言版本为 <code>preview</code>:</p>
<pre><code class="language-xml">&lt;PropertyGroup&gt;
&lt;LangVersion&gt;preview&lt;/LangVersion&gt;
&lt;/PropertyGroup&gt;
</code></pre>
<p>另外,Source Generators 需要引入两个 nuget 包:</p>
<pre><code class="language-xml">&lt;ItemGroup&gt;
&lt;PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0-3.final" PrivateAssets="all" /&gt;
&lt;PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0" PrivateAssets="all" /&gt;
&lt;/ItemGroup&gt;
</code></pre>
<h2 id="限制">限制</h2>
<p>Source Generators 仅能用于访问和生成代码,但是不能修改已有代码,这有一定原因是出于安全考量。</p>
<h2 id="文档">文档</h2>
<p>Source Generators 处于早期预览阶段,docs.microsoft.com 上暂时没有相关文档,关于它的文档请访问在 roslyn 仓库中的文档:</p>
<p>设计文档</p>
<p>使用文档</p>
<h2 id="后记">后记</h2>
<p>目前 Source Generators 仍处于非常早期的预览阶段,API 后期还可能会有很大的改动,因此现阶段不要用于生产。</p>
<p>另外,关于与 IDE 的集成、诊断信息、断点调试信息等的开发也在进行中,请期待后续的 preview 版本吧。</p><br><br>
来源:https://www.cnblogs.com/hez2010/p/12810993.html
頁: [1]
查看完整版本: C# 9 新特性:代码生成器、编译时反射