使用.NET源生成器(SG)实现一个自动注入的生成器

DI依赖注入对我们后端程序员来说肯定是基础中的基础了,我们经常会使用下面的代码注入相关的service

c# 复制代码
services.AddScoped<Biwen.AutoClassGen.TestConsole.Services.TestService2>();
services.AddTransient<Biwen.AutoClassGen.TestConsole.Services.TestService2>();
services.AddScoped<Biwen.AutoClassGen.TestConsole.Services.ITest2Service, Biwen.AutoClassGen.TestConsole.Services.TestService2>();
services.AddScoped<Biwen.AutoClassGen.TestConsole.Services.TestService3>();
services.AddScoped<Biwen.AutoClassGen.TestConsole.Services2.MyService>();
services.AddScoped<Biwen.AutoClassGen.TestConsole.Services.TestService>();
services.AddSingleton<Biwen.AutoClassGen.TestConsole.Services.ITestService, Biwen.AutoClassGen.TestConsole.Services.TestService>();
services.AddScoped<Biwen.AutoClassGen.TestConsole.Services.ITest2Service, Biwen.AutoClassGen.TestConsole.Services.TestService>();

对于上面的代码如果代码量很大 而且随着项目的迭代可能会堆积更多的代码,对于很多程序员来说第一想到的可能是透过反射批量注入,当然这也是最简单最直接的方式,今天我们使用源生成器的方式实现这个功能, 使用源生成器的方式好处还是有的 比如AOT需求,极致性能要求

实现这个功能的具体步骤:

定义Attribute-标注Attribute-遍历代码中标注Attribute的metadata集合-生成源代码

首先我们定义一个Attribute用于标注需要注入的类

c# 复制代码
namespace Biwen.AutoClassGen.Attributes
{
    using System;
    /// <summary>
    /// 服务生命周期
    /// </summary>
    public enum ServiceLifetime
    {
        Singleton = 1,
        Transient = 2,
        Scoped = 4,
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
    public class AutoInjectAttribute : Attribute
    {
        public ServiceLifetime ServiceLifetime { get; set; }
        public Type BaseType { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseType">NULL表示服务自身</param>
        /// <param name="serviceLifetime">服务生命周期</param>
        public AutoInjectAttribute(Type baseType = null, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            ServiceLifetime = serviceLifetime;
            BaseType = baseType;
        }
    }

//C#11及以上的版本支持泛型Attribute
#if NET7_0_OR_GREATER
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
    public class AutoInjectAttribute<T> : AutoInjectAttribute
    {
        public AutoInjectAttribute(ServiceLifetime serviceLifetime = ServiceLifetime.Scoped) : base(typeof(T), serviceLifetime)
        {
        }
    }
#endif
}

通过上面定义的Attribute我们就可以给我们的服务打上标记了

c# 复制代码
[AutoInject<TestService>]
[AutoInject<ITestService>(ServiceLifetime.Singleton)]
[AutoInject<ITest2Service>(ServiceLifetime.Scoped)]
public class TestService : ITestService, ITest2Service
{
	public string Say(string message)
	{
		return $"hello {message}";
	}
	public string Say2(string message)
	{
		return message;
	}
}

[AutoInject]
[AutoInject(serviceLifetime: ServiceLifetime.Transient)]
[AutoInject(typeof(ITest2Service), ServiceLifetime.Scoped)]
public class TestService2 : ITest2Service
{
	public string Say2(string message)
	{
		return message;
	}
}

接下来就是Roslyn分析C#语法解析代码片段:

实现源生成器的唯一接口IIncrementalGenerator 实现Initialize方法:

c# 复制代码
private const string AttributeValueMetadataNameInject = "AutoInject";

/// <summary>
/// 泛型AutoInjectAttribute
/// </summary>
private const string GenericAutoInjectAttributeName = "Biwen.AutoClassGen.Attributes.AutoInjectAttribute`1";

/// <summary>
/// 非泛型AutoInjectAttribute
/// </summary>
private const string AutoInjectAttributeName = "Biwen.AutoClassGen.Attributes.AutoInjectAttribute";


#region 非泛型

//使用SyntaxProvider的ForAttributeWithMetadataName得到所有标注的服务集合
var nodesAutoInject = context.SyntaxProvider.ForAttributeWithMetadataName(
	AutoInjectAttributeName,
	(context, attributeSyntax) => true,
	(syntaxContext, _) => syntaxContext.TargetNode).Collect();

IncrementalValueProvider<(Compilation, ImmutableArray<SyntaxNode>)> compilationAndTypesInject =
	context.CompilationProvider.Combine(nodesAutoInject);

#endregion

#region 泛型

var nodesAutoInjectG = context.SyntaxProvider.ForAttributeWithMetadataName(
GenericAutoInjectAttributeName,
(context, attributeSyntax) => true,
(syntaxContext, _) => syntaxContext.TargetNode).Collect();

IncrementalValueProvider<(Compilation, ImmutableArray<SyntaxNode>)> compilationAndTypesInjectG =
	context.CompilationProvider.Combine(nodesAutoInjectG);

#endregion

//合并所有的服务的编译类型
var join = compilationAndTypesInject.Combine(compilationAndTypesInjectG);

解下来我们定义一个Metadata类,该类主要定义Attribute的字段

c# 复制代码
private record AutoInjectDefine
{
	public string ImplType { get; set; } = null!;
	public string BaseType { get; set; } = null!;
	public string LifeTime { get; set; } = null!;
}

解析所有的标注泛型的Attribute metadata

c# 复制代码
private static List<AutoInjectDefine> GetGenericAnnotatedNodesInject(Compilation compilation, ImmutableArray<SyntaxNode> nodes)
{
	if (nodes.Length == 0) return [];
	// 注册的服务
	List<AutoInjectDefine> autoInjects = [];
	List<string> namespaces = [];

	foreach (ClassDeclarationSyntax node in nodes.AsEnumerable().Cast<ClassDeclarationSyntax>())
	{
		AttributeSyntax? attributeSyntax = null;
		foreach (var attr in node.AttributeLists.AsEnumerable())
		{
			var attrName = attr.Attributes.FirstOrDefault()?.Name.ToString();
			attributeSyntax = attr.Attributes.First(x => x.Name.ToString().IndexOf(AttributeValueMetadataNameInject, System.StringComparison.Ordinal) == 0);

			if (attrName?.IndexOf(AttributeValueMetadataNameInject, System.StringComparison.Ordinal) == 0)
			{
				//转译的Entity类名
				var baseTypeName = string.Empty;

				string pattern = @"(?<=<)(?<type>\w+)(?=>)";
				var match = Regex.Match(attributeSyntax.ToString(), pattern);
				if (match.Success)
				{
					baseTypeName = match.Groups["type"].Value.Split(['.']).Last();
				}
				else
				{
					continue;
				}

				var implTypeName = node.Identifier.ValueText;
				//var rootNamespace = node.AncestorsAndSelf().OfType<NamespaceDeclarationSyntax>().Single().Name.ToString();
				var symbols = compilation.GetSymbolsWithName(implTypeName);
				foreach (ITypeSymbol symbol in symbols.Cast<ITypeSymbol>())
				{
					implTypeName = symbol.ToDisplayString();
					break;
				}

				var baseSymbols = compilation.GetSymbolsWithName(baseTypeName);
				foreach (ITypeSymbol baseSymbol in baseSymbols.Cast<ITypeSymbol>())
				{
					baseTypeName = baseSymbol.ToDisplayString();
					break;
				}

				string lifeTime = "AddScoped"; //default
				{
					if (attributeSyntax.ArgumentList != null)
					{
						for (var i = 0; i < attributeSyntax.ArgumentList!.Arguments.Count; i++)
						{
							var expressionSyntax = attributeSyntax.ArgumentList.Arguments[i].Expression;
							if (expressionSyntax.IsKind(SyntaxKind.SimpleMemberAccessExpression))
							{
								var name = (expressionSyntax as MemberAccessExpressionSyntax)!.Name.Identifier.ValueText;
								lifeTime = name switch
								{
									"Singleton" => "AddSingleton",
									"Transient" => "AddTransient",
									"Scoped" => "AddScoped",
									_ => "AddScoped",
								};
								break;
							}
						}
					}

					autoInjects.Add(new AutoInjectDefine
					{
						ImplType = implTypeName,
						BaseType = baseTypeName,
						LifeTime = lifeTime,
					});

					//命名空间
					symbols = compilation.GetSymbolsWithName(baseTypeName);
					foreach (ITypeSymbol symbol in symbols.Cast<ITypeSymbol>())
					{
						var fullNameSpace = symbol.ContainingNamespace.ToDisplayString();
						// 命名空间
						if (!namespaces.Contains(fullNameSpace))
						{
							namespaces.Add(fullNameSpace);
						}
					}
				}
			}
		}
	}

	return autoInjects;
}

解析所有标注非泛型Attribute的metadata集合

c# 复制代码
private static List<AutoInjectDefine> GetAnnotatedNodesInject(Compilation compilation, ImmutableArray<SyntaxNode> nodes)
{
	if (nodes.Length == 0) return [];
	// 注册的服务
	List<AutoInjectDefine> autoInjects = [];
	List<string> namespaces = [];

	foreach (ClassDeclarationSyntax node in nodes.AsEnumerable().Cast<ClassDeclarationSyntax>())
	{
		AttributeSyntax? attributeSyntax = null;
		foreach (var attr in node.AttributeLists.AsEnumerable())
		{
			var attrName = attr.Attributes.FirstOrDefault()?.Name.ToString();
			attributeSyntax = attr.Attributes.FirstOrDefault(x => x.Name.ToString().IndexOf(AttributeValueMetadataNameInject, System.StringComparison.Ordinal) == 0);

			//其他的特性直接跳过
			if (attributeSyntax is null) continue;

			if (attrName?.IndexOf(AttributeValueMetadataNameInject, System.StringComparison.Ordinal) == 0)
			{
				var implTypeName = node.Identifier.ValueText;
				//var rootNamespace = node.AncestorsAndSelf().OfType<NamespaceDeclarationSyntax>().Single().Name.ToString();
				var symbols = compilation.GetSymbolsWithName(implTypeName);
				foreach (ITypeSymbol symbol in symbols.Cast<ITypeSymbol>())
				{
					implTypeName = symbol.ToDisplayString();
					break;
				}

				//转译的Entity类名
				var baseTypeName = string.Empty;

				if (attributeSyntax.ArgumentList == null || attributeSyntax.ArgumentList!.Arguments.Count == 0)
				{
					baseTypeName = implTypeName;
				}
				else
				{
					if (attributeSyntax.ArgumentList!.Arguments[0].Expression is TypeOfExpressionSyntax)
					{
						var eType = (attributeSyntax.ArgumentList!.Arguments[0].Expression as TypeOfExpressionSyntax)!.Type;
						if (eType.IsKind(SyntaxKind.IdentifierName))
						{
							baseTypeName = (eType as IdentifierNameSyntax)!.Identifier.ValueText;
						}
						else if (eType.IsKind(SyntaxKind.QualifiedName))
						{
							baseTypeName = (eType as QualifiedNameSyntax)!.ToString().Split(['.']).Last();
						}
						else if (eType.IsKind(SyntaxKind.AliasQualifiedName))
						{
							baseTypeName = (eType as AliasQualifiedNameSyntax)!.ToString().Split(['.']).Last();
						}
						if (string.IsNullOrEmpty(baseTypeName))
						{
							baseTypeName = implTypeName;
						}
					}
					else
					{
						baseTypeName = implTypeName;
					}
				}


				var baseSymbols = compilation.GetSymbolsWithName(baseTypeName);
				foreach (ITypeSymbol baseSymbol in baseSymbols.Cast<ITypeSymbol>())
				{
					baseTypeName = baseSymbol.ToDisplayString();
					break;
				}

				string lifeTime = "AddScoped"; //default
				{
					if (attributeSyntax.ArgumentList != null)
					{
						for (var i = 0; i < attributeSyntax.ArgumentList!.Arguments.Count; i++)
						{
							var expressionSyntax = attributeSyntax.ArgumentList.Arguments[i].Expression;
							if (expressionSyntax.IsKind(SyntaxKind.SimpleMemberAccessExpression))
							{
								var name = (expressionSyntax as MemberAccessExpressionSyntax)!.Name.Identifier.ValueText;
								lifeTime = name switch
								{
									"Singleton" => "AddSingleton",
									"Transient" => "AddTransient",
									"Scoped" => "AddScoped",
									_ => "AddScoped",
								};
								break;
							}
						}
					}

					autoInjects.Add(new AutoInjectDefine
					{
						ImplType = implTypeName,
						BaseType = baseTypeName,
						LifeTime = lifeTime,
					});

					//命名空间
					symbols = compilation.GetSymbolsWithName(baseTypeName);
					foreach (ITypeSymbol symbol in symbols.Cast<ITypeSymbol>())
					{
						var fullNameSpace = symbol.ContainingNamespace.ToDisplayString();
						// 命名空间
						if (!namespaces.Contains(fullNameSpace))
						{
							namespaces.Add(fullNameSpace);
						}
					}
				}
			}
		}
	}
	return autoInjects;
}

通过上面的两个方法我们就取到了所有的Attribute的metadata,接下来的代码其实就比较简单了 原理就是将metadata转换为形如以下的代码:

c# 复制代码
#pragma warning disable
public static partial class AutoInjectExtension
{
    /// <summary>
    /// 自动注册标注的服务
    /// </summary>
    /// <param name = "services"></param>
    /// <returns></returns>
    public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAutoInject(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)
    {
        services.AddScoped<Biwen.AutoClassGen.TestConsole.Services.TestService2>();
        services.AddTransient<Biwen.AutoClassGen.TestConsole.Services.TestService2>();
       // ...
        return services;
    }
}
#pragma warning restore

大致的代码如下:

c# 复制代码
context.RegisterSourceOutput(join, (ctx, nodes) =>
{
	var nodes1 = GetAnnotatedNodesInject(nodes.Left.Item1, nodes.Left.Item2);
	var nodes2 = GetGenericAnnotatedNodesInject(nodes.Right.Item1, nodes.Right.Item2);
	GenSource(ctx, [.. nodes1, .. nodes2]);
});

private static void GenSource(SourceProductionContext context, IEnumerable<AutoInjectDefine> injectDefines)
{
	// 生成代码
	StringBuilder classes = new();
	injectDefines.Distinct().ToList().ForEach(define =>
	{
		if (define.ImplType != define.BaseType)
		{
			classes.AppendLine($@"services.{define.LifeTime}<{define.BaseType}, {define.ImplType}>();");
		}
		else
		{
			classes.AppendLine($@"services.{define.LifeTime}<{define.ImplType}>();");
		}
	});

	string rawNamespace = string.Empty;
	//_namespaces.Distinct().ToList().ForEach(ns => rawNamespace += $"using {ns};\r\n");
	var envSource = Template.Replace("$services", classes.ToString());
	envSource = envSource.Replace("$namespaces", rawNamespace);
	// format:
	envSource = FormatContent(envSource);
	context.AddSource($"Biwen.AutoClassGenInject.g.cs", SourceText.From(envSource, Encoding.UTF8));
}
/// <summary>
/// 格式化代码
/// </summary>
/// <param name="csCode"></param>
/// <returns></returns>
private static string FormatContent(string csCode)
{
	var tree = CSharpSyntaxTree.ParseText(csCode);
	var root = tree.GetRoot().NormalizeWhitespace();
	var ret = root.ToFullString();
	return ret;
}

private const string Template = """
	// <auto-generated />
	// issue:https://github.com/vipwan/Biwen.AutoClassGen/issues
	// 如果你在使用中遇到问题,请第一时间issue,谢谢!
	// This file is generated by Biwen.AutoClassGen.AutoInjectSourceGenerator
	#pragma warning disable
	$namespaces
	public static partial class AutoInjectExtension
	{
		/// <summary>
		/// 自动注册标注的服务
		/// </summary>
		/// <param name="services"></param>
		/// <returns></returns>
		public static  Microsoft.Extensions.DependencyInjection.IServiceCollection AddAutoInject(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)
		{
			$services
			return services;
		}
	}
	
	#pragma warning restore
	""";

最终工具会自动为你生成以下代码:

c# 复制代码
// <auto-generated />
// issue:https://github.com/vipwan/Biwen.AutoClassGen/issues
// 如果你在使用中遇到问题,请第一时间issue,谢谢!
// This file is generated by Biwen.AutoClassGen.AutoInjectSourceGenerator
#pragma warning disable
public static partial class AutoInjectExtension
{
    /// <summary>
    /// 自动注册标注的服务
    /// </summary>
    /// <param name = "services"></param>
    /// <returns></returns>
    public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddAutoInject(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)
    {
        services.AddScoped<Biwen.AutoClassGen.TestConsole.Services.TestService2>();
        services.AddTransient<Biwen.AutoClassGen.TestConsole.Services.TestService2>();
		//...
        return services;
    }
}
#pragma warning restore

以上代码就完成了整个源生成步骤,最后你可以使用我发布的nuget包体验:

bash 复制代码
dotnet add package Biwen.AutoClassGen

源代码我发布到了GitHub,欢迎star! https://github.com/vipwan/Biwen.AutoClassGen

相关推荐
万雅虎15 天前
使用Roslyn的源生成器生成DTO
roslyn·csharp·sg
万雅虎2 个月前
使用 `Roslyn` 分析器和修复器 对异步方法规范化返回Async结尾
netcore·roslyn·csharp·sg
万雅虎3 个月前
使用.NET源生成器(SG)生成项目的版本号信息
roslyn·csharp·sg
@背包3 个月前
C# 版本八大排序算法|.Net 架构
算法·排序算法·csharp
小康师兄4 个月前
IoTDB 入门教程 实战篇④——C#示例(开源)
开源·c#·时序数据库·iotdb·csharp
万雅虎4 个月前
C#中使用 record 的好处 因为好用所以推荐~
csharp
万雅虎4 个月前
如何在.NET Framework,或NET8以前的项目中使用C# 12的新特性
csharp
万雅虎4 个月前
C# 使用模式匹配的好处,因为好用所以推荐~
csharp
VAllen4 个月前
【一天一点.NET小知识】运用向量Vector<T>加速求和计算
c#·.net·.net core·dotnet·csharp
tokengo4 个月前
从Java开发者到.NET Core初级工程师学习路线:C#语言基础
java·csharp·新人入门