XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.AutoImplement

【DLL】自动生成实现类

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.AutoImplement

更换部分完成

ae2b90f
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

2 个文件 +16 -14
Modified XFEExtension.NetCore.AutoImplement.Analyzer/Generator/ImplementAutoGenerator.cs +14 -12
@@ -6,15 +6,16 @@ using System.Linq;
6 6 namespace XFEExtension.NetCore.AutoImplement.Analyzer.Generator
7 7 {
8 8 [Generator]
9 public class ImplementAutoGenerator : ISourceGenerator
9 public class ImplementAutoGenerator : IIncrementalGenerator
10 10 {
11 public void Initialize(GeneratorInitializationContext context)
11 public void Initialize(IncrementalGeneratorInitializationContext context)
12 12 {
13 context.RegisterSourceOutput(context.CompilationProvider, Generate);
13 14 }
14 15
15 public void Execute(GeneratorExecutionContext context)
16 public void Generate(SourceProductionContext context, Compilation compilation)
16 17 {
17 var syntaxTrees = context.Compilation.SyntaxTrees;
18 var syntaxTrees = compilation.SyntaxTrees;
18 19 foreach (var syntaxTree in syntaxTrees)
19 20 {
20 21 var root = syntaxTree.GetRoot();
@@ -28,7 +29,7 @@ namespace XFEExtension.NetCore.AutoImplement.Analyzer.Generator
28 29 foreach (var classDeclaration in classDeclarations)
29 30 {
30 31 var className = classDeclaration.Identifier.ValueText;
31 var implementationSyntaxTree = GenerateImplementationSyntaxTree(classDeclaration, usingDirectives, fileScopedNamespaceDeclarationSyntax);
32 var implementationSyntaxTree = GenerateImplementationSyntaxTree(classDeclaration, usingDirectives, fileScopedNamespaceDeclarationSyntax, className);
32 33 context.AddSource($"{className}Impl.g.cs", implementationSyntaxTree.ToString());
33 34 }
34 35 }
@@ -36,24 +37,25 @@ namespace XFEExtension.NetCore.AutoImplement.Analyzer.Generator
36 37
37 38 private static bool IsCreateImplAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "CreateImpl");
38 39
39 private static SyntaxTree GenerateImplementationSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes, FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax)
40 private static SyntaxTree GenerateImplementationSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes, FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax, string className, string[] modifierParameters, string nameSpace)
40 41 {
41 var className = classDeclaration.Identifier.ValueText;
42 var fatherClassName = classDeclaration.Identifier.ValueText;
43 var modifiers = modifierParameters.Select(modifier => SyntaxFactory.ParseToken(modifier)).ToArray();
42 44 ClassDeclarationSyntax implementationClass;
43 45 if (classDeclaration.ParameterList is null)
44 46 {
45 implementationClass = SyntaxFactory.ClassDeclaration($"{className}Impl")
46 .AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword), SyntaxFactory.Token(SyntaxKind.SealedKeyword))
47 implementationClass = SyntaxFactory.ClassDeclaration(className)
48 .AddModifiers(modifiers)
47 49 .AddMembers(classDeclaration.Members.OfType<ConstructorDeclarationSyntax>().Select(constructor =>
48 50 {
49 return SyntaxFactory.ConstructorDeclaration($"{className}Impl")
51 return SyntaxFactory.ConstructorDeclaration(className)
50 52 .AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
51 53 .WithBody(SyntaxFactory.Block())
52 54 .WithParameterList(constructor.ParameterList)
53 55 .WithInitializer(SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(constructor.ParameterList.Parameters.Select(parameter => SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier)))))));
54 56 }).ToArray())
55 57 .WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia($@"/// <summary>
56 /// <seealso cref=""{className}Impl""/> 是根据 <seealso cref=""{className}""/> 自动生成的实现类
58 /// <seealso cref=""{className}""/> 是根据 <seealso cref=""{fatherClassName}""/> 自动生成的实现类
57 59 /// </summary>
58 60 "))
59 61 .NormalizeWhitespace();
@@ -61,7 +63,7 @@ namespace XFEExtension.NetCore.AutoImplement.Analyzer.Generator
61 63 else
62 64 {
63 65 implementationClass = SyntaxFactory.ClassDeclaration($"{className}Impl")
64 .AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
66 .AddModifiers(modifiers)
65 67 .AddMembers(SyntaxFactory.ConstructorDeclaration($"{className}Impl")
66 68 .AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
67 69 .WithBody(SyntaxFactory.Block())
Modified XFEExtension.NetCore.AutoImplement/CreateImpl.cs +2 -2
@@ -8,13 +8,13 @@ public class CreateImpl : Attribute
8 8 {
9 9 public string? ClassName { get; set; }
10 10 public string? NameSpace { get; set; }
11 public string? Modifier { get; set; }
11 public string[]? Modifier { get; set; }
12 12 public CreateImpl() { }
13 13 public CreateImpl(string className)
14 14 {
15 15 ClassName = className;
16 16 }
17 public CreateImpl(string className, string modifier)
17 public CreateImpl(string className, params string[] modifier)
18 18 {
19 19 ClassName = className;
20 20 Modifier = modifier;