Fireasy3 揭秘 -- 使用 SourceGeneraor 改进服务发现
目录[*]Fireasy3 揭秘 -- 依赖注入与服务发现
[*]Fireasy3 揭秘 -- 自动服务部署
[*]Fireasy3 揭秘 -- 使用 SourceGeneraor 改进服务发现
[*]Fireasy3 揭秘 -- 使用 SourceGeneraor 实现动态代理(AOP)
在 Fireasy3 揭秘 -- 依赖注入与服务发现 这篇中,我们通过遍列程序集中的所有类,来查找三个类型的服务接口,这样应用在启动时会消耗一定的时间来处理这些事情。今天,我们将用 ISourceGenerator 来对它进行改进。
ISourceGenerator 是 Microsoft.CodeAnalysis.Analyzers 中的一项技术,它是基于代码分析的原理,在语法树中查找所需要的内容,通过这些内容再构造一段源代码,使得我们在编译程序集的时候,把这些代码一并编译进去。使用它的好处在于,它是在编译时生成的,而不像 Emit 或其他反射等方法来构建的动态代码一样,在运行时将耗费一定的性能。
需要新建一个 .net standard 2.0 的项目,并引入 Microsoft.CodeAnalysis.Analyzers 和 Microsoft.CodeAnalysis.CSharp,见 Fireasy.Common.Analyzers。
在项目里添加一个类,实现 ISourceGenerator 接口,如下:
public class ServiceDiscoverGenerator : ISourceGenerator
{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>void ISourceGenerator.Initialize(GeneratorInitializationContext context)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> Debugger.Launch();
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> context.RegisterForSyntaxNotifications(() => new ServiceDiscoverSyntaxReceiver());
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>void ISourceGenerator.Execute(GeneratorExecutionContext context)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
} Initialize 方法用于初始化生成器,使用 RegisterForSyntaxNotifications 方法向上下文注入一个语法接收器,以便用来分析语法树。这里的语法接收器有两种,分别是 ISyntaxReceiver 和 ISyntaxContextReceiver,后者可以从上下文中获取到 SemanticModel 对象,这样的话能够从语法节点中获取到定义的符号模型。使用符号模型相对于语法节点来说要更方便一些。下面是基于 ISyntaxContextReceiver 接口的语法接收器。
internal class ServiceDiscoverSyntaxReceiver : ISyntaxContextReceiver
{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private const string SingletonServiceName = "Fireasy.Common.DependencyInjection.ISingletonService";
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private const string TransientServiceName = "Fireasy.Common.DependencyInjection.ITransientService";
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private const string ScopedServiceName = "Fireasy.Common.DependencyInjection.IScopedService";
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private const string RegisterAttributeName = "Fireasy.Common.DependencyInjection.ServiceRegisterAttribute";
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private List<ClassMetadata> _metadatas = new();
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>void ISyntaxContextReceiver.OnVisitSyntaxNode(GeneratorSyntaxContext context)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> if (context.Node is ClassDeclarationSyntax classSyntax)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>AnalyseClassSyntax(context.SemanticModel, classSyntax);
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
} OnVisitSyntaxNode 方法正如 lambda 表达式树的 ExpressionVisitor 一样,语法树中的每一个节点都会被它访问到。我们需要分析的是类,因此只需要处理 ClassDeclarationSyntax 语法即可。AnalyseClassSyntax 方法如下:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// 分析类型语法。
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// </summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <param name="model"></param>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <param name="syntax"></param>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private void AnalyseClassSyntax(SemanticModel model, ClassDeclarationSyntax syntax)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> var typeSymbol = (ITypeSymbol)model.GetDeclaredSymbol(syntax)!;
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> var interfaces = typeSymbol.Interfaces;
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> //判断是否使用了 特殊
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> var regAttr = typeSymbol.GetAttributes().FirstOrDefault(s => s.AttributeClass!.ToDisplayString() == RegisterAttributeName);
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> var lifetime = string.Empty;
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> if (regAttr != null)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>lifetime = GetLifetime((int)regAttr.ConstructorArguments.Value!);
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> else if (interfaces.Any(s => s.ToDisplayString() == SingletonServiceName))
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>lifetime = "Singleton";
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> else if (interfaces.Any(s => s.ToDisplayString() == TransientServiceName))
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>lifetime = "Transient";
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> else if (interfaces.Any(s => s.ToDisplayString() == ScopedServiceName))
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>lifetime = "Scoped";
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> if (!string.IsNullOrEmpty(lifetime))
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>var serviceTypes = GetServiceTypes(interfaces).ToList();
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>//如果没有实现任何接口,则判断基类是不是抽象类,如果不是,则注册自己
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>if (serviceTypes.Count == 0 && (typeSymbol.BaseType?.Name == "Object" || typeSymbol.BaseType?.IsAbstract == false))
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><None Include="..\Fireasy.Common.Analyzers\bin\$(Configuration)\**\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>
</Target>
</Project>serviceTypes.Add(typeSymbol);
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>_metadatas.Add(new ClassMetadata(typeSymbol, lifetime).AddServiceTypes(serviceTypes));
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// 获取生命周期。
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// </summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <param name="value"></param>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <returns></returns>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private string GetLifetime(int value) => value switch
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> 0 => "Singleton",
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> 1 => "Scoped",
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> 2 => "Transient",
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> _ => string.Empty
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>};
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// 从接口中筛选出服务类。
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// </summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <param name="types"></param>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <returns></returns>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private IEnumerable<ITypeSymbol> GetServiceTypes(IEnumerable<INamedTypeSymbol> types)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> foreach (var type in types)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>if (type.ToDisplayString() == SingletonServiceName ||
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><None Include="..\Fireasy.Common.Analyzers\bin\$(Configuration)\**\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>
</Target>
</Project>type.ToDisplayString() == TransientServiceName ||
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><None Include="..\Fireasy.Common.Analyzers\bin\$(Configuration)\**\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>
</Target>
</Project>type.ToDisplayString() == ScopedServiceName)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><None Include="..\Fireasy.Common.Analyzers\bin\$(Configuration)\**\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>
</Target>
</Project>continue;
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>yield return type;
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>} 至此,我们就得到了一份可注册的元数据,它由一个实现类对应多个服务类。ClassMetadata 的定义如下:
/// <summary>
/// 类的元数据。
/// </summary>
public class ClassMetadata
{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// 初始化 <see cref="ClassMetadata"/> 类的新实例。
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// </summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <param name="implementationType">实现类的类型。</param>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <param name="lifetime">生命周期。</param>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>public ClassMetadata(ITypeSymbol implementationType, string lifetime)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> ImplementationType = implementationType;
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> Lifetime = lifetime;
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// 获取实现类的类型。
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// </summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>public ITypeSymbol ImplementationType { get; }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// 获取服务类的类型列表。
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// </summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>public List<ITypeSymbol> ServiceTypes { get; } = new();
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// 获取生命周期。
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// </summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>public string Lifetime { get; }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// 添加服务类型。
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// </summary>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <param name="serviceTypes">服务类型列表。</param>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>/// <returns></returns>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>public ClassMetadata AddServiceTypes(IEnumerable<ITypeSymbol> serviceTypes)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> ServiceTypes.AddRange(serviceTypes);
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> return this;
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
} 好了,得到这一份元数据后,我们转到 ServiceDiscoverGenerator,看看下一步它要做什么。
public class ServiceDiscoverGenerator : ISourceGenerator
{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>void ISourceGenerator.Initialize(GeneratorInitializationContext context)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> Debugger.Launch();
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> context.RegisterForSyntaxNotifications(() => new ServiceDiscoverSyntaxReceiver());
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>void ISourceGenerator.Execute(GeneratorExecutionContext context)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> if (context.SyntaxContextReceiver is ServiceDiscoverSyntaxReceiver receiver)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>var metadatas = receiver.GetMetadatas();
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>if (metadatas.Count > 0)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><None Include="..\Fireasy.Common.Analyzers\bin\$(Configuration)\**\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>
</Target>
</Project>context.AddSource("ServicesDiscover.cs", BuildDiscoverSourceCode(metadatas));
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
} 在 Execute 方法中,拿到接收器分析出来的元数据,通过 BuildDiscoverSourceCode 方法去生成一段源代码。它是一个服务部署类,在 Configure 方法中,会把所有的服务描述添加到 IServiceCollection 容器内,如下:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private SourceText BuildDiscoverSourceCode(List<ClassMetadata> metadatas)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> var sb = new StringBuilder();
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> sb.AppendLine(@"
using Fireasy.Common.DependencyInjection;
using Fireasy.Common.DynamicProxy;
using Microsoft.Extensions.DependencyInjection;
namespace __ServiceDiscoverNs
{
internal class __ServiceDiscoverServicesDeployer: IServicesDeployer
{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>void IServicesDeployer.Configure(IServiceCollection services)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{");
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> foreach (var metadata in metadatas)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>foreach (var svrType in metadata.ServiceTypes)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><None Include="..\Fireasy.Common.Analyzers\bin\$(Configuration)\**\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>
</Target>
</Project>sb.AppendLine($"<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> services.Add{metadata.Lifetime}(typeof({GetTypeName(svrType)}), typeof({GetTypeName(metadata.ImplementationType)}));");
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> sb.AppendLine(@"
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
}
}");
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> return SourceText.From(sb.ToString(), Encoding.UTF8);
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>private string GetTypeName(ITypeSymbol symbol)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> if (symbol is INamedTypeSymbol namedTypeSymbol)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> {
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>//如果是泛型,要处理成 Any<> 或 Any<,> 这样的描述
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>if (namedTypeSymbol.IsGenericType)
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>{
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><None Include="..\Fireasy.Common.Analyzers\bin\$(Configuration)\**\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>
</Target>
</Project>var t = namedTypeSymbol.ToDisplayString();
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><None Include="..\Fireasy.Common.Analyzers\bin\$(Configuration)\**\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>
</Target>
</Project>return t.Substring(0, t.IndexOf("<") + 1) + new string(',', namedTypeSymbol.TypeArguments.Length - 1) + ">";
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>}
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> }
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> return symbol.ToDisplayString();
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>} 到这里,源代码生成器就算是完成了,那接下来怎么让它工作呢?
首先,我们需要找到一个“宿主”,我之所以这么称呼,是因为 nuget 打包时,需要将分析器依附到一个包内,因此我选择 Fireasy.Common,在 Fireasy.Common 的项目文件中,加下以下一段代码,它的目的是当 Fireasy.Common 打包时,Fireasy.Common.Analyzers.dll 会自动打包到 analyzers 目录下,引用 Fireasy.Common 包时,会自动使用该分析器来生成代码。如下:
<Project Sdk="Microsoft.NET.Sdk">
<Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">
<ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project><None Include="..\Fireasy.Common.Analyzers\bin\$(Configuration)\**\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>
</Target>
</Project> 我们测试的时候,因为是直接引用的项目,因此需要引用包含分析器的项目,而且要加上 OutputItemType 和 ReferenceOutputAssembly,如下:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\libraries\Fireasy.Common.Analyzers\Fireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project> 好了,编译测试项目,使用 ILSpy 反编译 dll 文件,你会发现,实现了 ISingletonService、ITransientService 或 IScopedService 的类自动注册进来了:
// __ServiceDiscoverNs.__ServiceDiscoverServicesDeployer
using Fireasy.Common.DependencyInjection;
using Fireasy.Common.Tests;
using Microsoft.Extensions.DependencyInjection;
void IServicesDeployer.Configure(IServiceCollection services)
{
services.AddSingleton(typeof(DependencyInjectionTests.ITestSingletonService), typeof(DependencyInjectionTests.TestSingletonServiceImpl));
services.AddTransient(typeof(DependencyInjectionTests.ITestTransientService), typeof(DependencyInjectionTests.TestTransientServiceImpl));
services.AddScoped(typeof(DependencyInjectionTests.ITestScopedService), typeof(DependencyInjectionTests.TestScopedServiceImpl));
services.AddTransient(typeof(DependencyInjectionTests.ITestWithRegisterAttr), typeof(DependencyInjectionTests.TestWithRegisterAttrImpl));
services.AddTransient(typeof(DependencyInjectionTests.TestWithRegisterAttrNonIntefaceImpl), typeof(DependencyInjectionTests.TestWithRegisterAttrNonIntefaceImpl));
services.AddTransient(typeof(DependencyInjectionTests.IGenericService<, >), typeof(DependencyInjectionTests.GenericService<, >));
services.AddTransient(typeof(DependencyInjectionTests.TestDynamicProxyClass), typeof(DependencyInjectionTests.TestDynamicProxyClass));
services.AddTransient(typeof(ObjectActivatorTests.ITestService), typeof(ObjectActivatorTests.TestService));
} 另外还有一个小窍门,在测试项目的“依赖项”--“分析器”下,你会看到一个属于自己的分析器,依次展开,也会找到所生成的那个代码文件。
最后,奉上 Fireasy 3 的开源地址:https://gitee.com/faib920/fireasy3 ,欢迎大家前来捧场。
本文相关代码请参考:
https://gitee.com/faib920/fireasy3/src/libraries/Fireasy.Common.Analyzers/ServiceDiscover
https://gitee.com/faib920/fireasy3/tests/Fireasy.Common.Tests/DependencyInjectionTests.cs
更多内容请移步官网 http://www.fireasy.cn 。
来源:https://www.cnblogs.com/fireasy/archive/2023/03/03/17174121.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]