Source Generator實戰


前言

最近刷B站的時候瀏覽到了老楊的關於Source Generator的簡介視頻。其實當初.Net 6剛發布時候看到過微軟介紹這個東西,但並沒有在意。因為粗看覺得這東西限制蠻多的,畢竟C#是強類型語言,有些動態的東西不好操作,而且又有Fody、Natasha這些操作IL的庫。

最近寫前端比較多,看到這個這個,都是自動引入相關包,極大的提高了我開發前端的舒適度。又聯想到隔壁Java的有Lombok,用起來都很香。搜了一下也沒看到C#有相關的東西,於是決定自己動手開發一個,提高C#開發體驗。

實現一個Source Generator

這里不對Source Generator做基本的使用介紹,直接實操。如果需要了解相關信息,建議直接看官方文檔或者去搜索相關文章。

首先我們看一下效果,假如我的代碼是

namespace SourceGenerator.Demo
{
    public partial class UserClass
    {
        [Property]
        private string _test;
    }
}

那么,最終生成的應該是

// Auto-generated code
namespace SourceGenerator.Demo
{
    public partial class UserClass
    {
        public string Test { get => _test; set => _test = value; }
    }
}

我們按最簡單的實現來考慮,那么只需要

  1. 在語法樹中找到field
  2. 找到字段的class、namespace
  3. 生成代碼

第一步

首先我們來看第一步。第一步需要找到field,這個我們借助Attribute的特性,能夠很快的找到,在SourceGenerator中只需要判斷一下Attribute的名字即可
定義一個SyntaxReciver,然后在SourceGenerator中注冊一下

// file: PropertyAttribute.cs
using System;

namespace SourceGenerator.Common
{
    [AttributeUsage(AttributeTargets.Field)]
    public class PropertyAttribute : Attribute
    {
        public const string Name = "Property";
    }
}
// file: AutoPropertyReceiver.cs
public class AutoPropertyReceiver : ISyntaxReceiver
{
    public List<AttributeSyntax> AttributeSyntaxList { get; } = new List<AttributeSyntax>();

    public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
    {
        if (syntaxNode is AttributeSyntax cds && cds.Name is IdentifierNameSyntax identifierName &&
            (
                identifierName.Identifier.ValueText == PropertyAttribute.Name ||
                identifierName.Identifier.ValueText == nameof(PropertyAttribute))
           )
        {
            AttributeSyntaxList.Add(cds);
        }
    }
}

// file: AutoPropertyGenerator.cs
[Generator]
public class AutoPropertyGenerator : ISourceGenerator
{
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => new AutoPropertyReceiver());
    }

    // other code
    ...
}

第二步

第二步就是SyntaxTree的查找,熟悉SyncaxTree的話比較容易完成

public void Execute(GeneratorExecutionContext context)
{
    var syntaxReceiver = (AutoPropertyReceiver)context.SyntaxReceiver;
    var attributeSyntaxList = syntaxReceiver.AttributeSyntaxList;

    if (attributeSyntaxList.Count == 0)
    {
        return;
    }

    // 保存一下類名,因為一個類中可能有有多個字段生成,這里去掉重復
    var classList = new List<string>();
    foreach (var attributeSyntax in attributeSyntaxList)
    {
        // 找到class,並且判斷一下是否有parital字段
        var classDeclarationSyntax = attributeSyntax.FirstAncestorOrSelf<ClassDeclarationSyntax>();
        if (classDeclarationSyntax == null ||
            !classDeclarationSyntax.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)))
        {
            continue;
        }

        // 找到namespace
        var namespaceDeclarationSyntax =
            classDeclarationSyntax.FirstAncestorOrSelf<BaseNamespaceDeclarationSyntax>();

        if (classList.Contains(classDeclarationSyntax.Identifier.ValueText))
        {
            continue;
        }

        // 找到field
        var fieldDeclarationList = classDeclarationSyntax.Members.OfType<FieldDeclarationSyntax>().ToList();
        if (fieldDeclarationList.Count == 0)
        {
            continue;
        }
        // 其他代碼
        ...
    }
}

第三步

第三步就是簡單粗暴的根據第二步中拿到的信息,拼一下字符串。

當然其實拼字符串是很不好的行為,最好是用模板去實現,其次就算是拼字符串也理應用StringBuilder,但這里只是做一個Demo,無所謂了

public void Execute(GeneratorExecutionContext context)
{
        ...
        // 上面是第二步的代碼
        // 拼源代碼字符串
        var source = $@"// Auto-generated code

namespace {namespaceDeclarationSyntax.Name.ToString()}
{{
public partial class {classDeclarationSyntax.Identifier}
{{";
        var propertyStr = "";
        foreach (var fieldDeclaration in fieldDeclarationList)
        {
            var variableDeclaratorSyntax = fieldDeclaration.Declaration.Variables.FirstOrDefault();

            var fieldName = variableDeclaratorSyntax.Identifier.ValueText;
            var propertyName = GetCamelCase(fieldName);

            propertyStr += $@"
public string {propertyName} {{ get => {fieldName}; set => {fieldName} = value; }}";
        }

        source += propertyStr;
        source += @"
}
}
";
        // 添加到源代碼,這樣IDE才能感知
        context.AddSource($"{classDeclarationSyntax.Identifier}.g.cs", source);
        // 保存一下類名,避免重復生成
        classList.Add(classDeclarationSyntax.Identifier.ValueText);
    }
}

使用

寫一個測試類

using SourceGenerator.Common;

namespace SourceGenerator.Demo;

public partial class UserClass
{
    [Property] private string _test = "test";

    [Property] private string _test2;
}

然后重啟IDE,可以看到效果,並且直接調用屬性是不報錯的
image
image

結尾

這里僅演示了最基本的Source Generator的功能,限於篇幅也無法深入講解,上面的代碼可以在這里查看,目前最新的代碼還實現了字段生成構造函數,appsettings.json生成AppSettings常量字段類。

如果你只是想使用,可以直接nuget安裝SourceGenerator.Library

以下為個人觀點

Source Generator在我看來最大的價值在於提供開發時的體驗。至於性能,可以用Fody等庫Emit IL代碼,功能更強大更完善,且沒有分部類的限制。但此類IL庫最大的問題在Design-Time時無法拿到生成后的代碼,導致需要用一些奇奇怪怪的方法去用生成代碼。

Source Generator未來可以做的事情有很多,比如

  1. ORM實體映射
    如果數據庫是Code First,那么其實還好。但如果說是Db First,主流的ORM庫都是通過命令去生成Model的,但命令通常我記不住,因為用的頻率並不高。
    如果后期加字段,要么我重新生成一次,我又得去找這個命令。要么我手動去C#代碼中加這個字段,我能保證自己可以寫正確,但是團隊其他成員呢?
  2. 結合Emit IL技術
    上面其實說了Emit是無法在Design-Time中使用的,但如果我們使用Source Generator創建一些空的方法,然后用IL去改寫,應該可以解決這個問題
  3. 依賴注入
    目前而言我們在Asp.net Core中創建了服務,那么我們需要AddSingleton等方法添加進去,這個其實很痛苦,因為首先會顯得代碼很長,其次這個操作很無聊且容易遺漏。
    現在主流的框架都是通過Assembly掃描的方式去動態注冊,避免手動去添加服務。但如果通過Source Generator掃碼這些類,就可以在編譯時添加進DI容器
  4. 對象映射
    Java里面有個庫叫做MapStruct,原理是用maven插件生成靜態的java代碼,然后按字段賦值。C#里面我好像沒有看到這種方法,目前我用過的Automapper和Tinymapper都是先去做Bind,然后再使用。(插個題外話,Tinymapper以前的版本是不需要Bind,直接用的,但后來就要了,似乎是為了解決多線程的問題)
    Bind其實很痛苦,我很討厭寫這種樣板代碼,以至於我根本就不想用這類Mapper,直接Json Copy。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM