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

XFEExtension.NetCore.AutoImplement

【DLL】自动生成实现类

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

XFEstudio/XFEExtension.NetCore.AutoImplement

debug

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

代码差异

4 个文件 +40 -6
Modified XFEExtension.NetCore.AutoImplement.Analyzer/Generator/ImplementAutoGenerator.cs +24 -4
@@ -1,6 +1,7 @@
1 1 using Microsoft.CodeAnalysis;
2 2 using Microsoft.CodeAnalysis.CSharp;
3 3 using Microsoft.CodeAnalysis.CSharp.Syntax;
4 using System;
4 5 using System.Diagnostics;
5 6 using System.Linq;
6 7 using System.Threading;
@@ -13,15 +14,17 @@ public class ImplementAutoGenerator : IIncrementalGenerator
13 14 {
14 15 public void Initialize(IncrementalGeneratorInitializationContext context)
15 16 {
17 //var implementationProvider = context.SyntaxProvider.ForAttributeWithMetadataName("CreateImpl",
18 //static (x, _) => x is ClassDeclarationSyntax classDeclaration && classDeclaration.AttributeLists.Any(IsCreateImplAttribute),
19 //GetCreateImpl);
16 20 var implementationProvider = context.SyntaxProvider.ForAttributeWithMetadataName("CreateImpl",
17 static (x, _) => x is ClassDeclarationSyntax classDeclaration && classDeclaration.AttributeLists.Any(IsCreateImplAttribute),
21 static (x, _) => x is ClassDeclarationSyntax classDeclaration,
18 22 GetCreateImpl);
19 23 context.RegisterSourceOutput(implementationProvider.Combine(context.CompilationProvider), Generate);
20 24 }
21 25
22 26 public void Generate(SourceProductionContext context, (ImplementationInfo? ImplementationInfo, Compilation Compilation) args)
23 27 {
24 Debugger.Launch();
25 28 if (args.ImplementationInfo is null)
26 29 return;
27 30 var root = args.ImplementationInfo.ClassDeclaration.SyntaxTree.GetRoot();
@@ -40,7 +43,8 @@ public class ImplementAutoGenerator : IIncrementalGenerator
40 43
41 44 public static ImplementationInfo? GetCreateImpl(GeneratorAttributeSyntaxContext context, CancellationToken token)
42 45 {
43 Debugger.Launch();
46 if (context is { TargetSymbol: INamedTypeSymbol } && context.Attributes.First().ConstructorArguments.Length > 0)
47 Debugger.Launch();
44 48 token.ThrowIfCancellationRequested();
45 49 var classDeclaration = (ClassDeclarationSyntax)context.TargetNode;
46 50 var attributeData = context.Attributes.FirstOrDefault(ad => ad.AttributeClass?.ToDisplayString() == "CreateImpl");
@@ -48,8 +52,24 @@ public class ImplementAutoGenerator : IIncrementalGenerator
48 52 return null;
49 53 var className = classDeclaration.Identifier.ValueText;
50 54 var targetClassName = attributeData.NamedArguments.FirstOrDefault(kv => kv.Key == "ClassName").Value.Value as string ?? $"{className}Impl";
55 //var targetClassName = $"{className}Impl";
51 56 var nameSpace = attributeData.NamedArguments.FirstOrDefault(kv => kv.Key == "NameSpace").Value.Value as string ?? string.Empty;
52 var modifiers = attributeData.NamedArguments.FirstOrDefault(kv => kv.Key == "Modifiers").Value.Values.Select(v => v.Value?.ToString() ?? string.Empty).Where(s => !string.IsNullOrEmpty(s)).ToArray() ?? [];//此处出现问题
57 //var nameSpace = string.Empty;
58 //var value = attributeData.NamedArguments.FirstOrDefault(kv => kv.Key == "Modifiers").Value;
59 //var modifiers = attributeData.NamedArguments.FirstOrDefault(kv => kv.Key == "Modifiers").Value.Values.Select(v => v.Value?.ToString() ?? string.Empty).Where(s => !string.IsNullOrEmpty(s)).ToArray() ?? [];
60 var modifiers = Array.Empty<string>();
61 //foreach (var attributeList in classDeclaration.AttributeLists)
62 //{
63 // foreach (var attribute in attributeList.Attributes)
64 // {
65 // if (attribute is null || attribute.Name.ToString() != "CreateImpl")
66 // continue;
67 // foreach (var argument in attribute.ArgumentList.Arguments)
68 // {
69
70 // }
71 // }
72 //}
53 73 if (modifiers.Length == 0)
54 74 modifiers = ["internal", "partial"];
55 75 return new ImplementationInfo
Added XFEExtension.NetCore.AutoImplement.Analyzer/Model/TargetImplementationInfo.cs +8 -0
@@ -0,0 +1,8 @@
1 namespace XFEExtension.NetCore.AutoImplement.Analyzer.Model;
2
3 public class TargetImplementationInfo
4 {
5 public string TargetClassName { get; set; } = string.Empty;
6 public string[] Modifiers { get; set; } = [];
7 public string NameSpace { get; set; } = string.Empty;
8 }
Modified XFEExtension.NetCore.AutoImplement.Test/MyTestClass.cs +3 -1
@@ -1,6 +1,8 @@
1 1 namespace XFEExtension.NetCore.AutoImplement.Test;
2 2
3 [CreateImpl]
3 [CreateImpl("MyTestClassA")]
4 [CreateImpl("MyTestClassB", Modifiers = ["public", "partial"])]
5 [CreateImpl("MyTestClassC", Modifiers = ["public", "partial"])]
4 6 public abstract class MyTestClass<T, F>(T obj, F obj2) where T : class where F : new()
5 7 {
6 8 public T TProperty { get; set; } = obj;
Modified XFEExtension.NetCore.AutoImplement/CreateImpl.cs +5 -1
@@ -3,11 +3,15 @@
3 3 /// <summary>
4 4 /// 创建一个类的实现类
5 5 /// </summary>
6 [AttributeUsage(AttributeTargets.Class)]
6 [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
7 7 public class CreateImpl : Attribute
8 8 {
9 9 public string? ClassName { get; set; }
10 10 public string? NameSpace { get; set; }
11 11 public string[]? Modifiers { get; set; }
12 12 public CreateImpl() { }
13 public CreateImpl(string className)
14 {
15 ClassName = className;
16 }
13 17 }