XFEExtension
【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等
关注
0
Fork
0
Star
0
返回提交历史
Added
XFE各类拓展.NetCore.Analyzer.Test/SystemProfile.cs
+11
-0
Deleted
XFE各类拓展.NetCore.Analyzer.Test/TestAbstractClass.cs
+0
-11
Modified
XFE各类拓展.NetCore.Analyzer.Test/TestClass.cs
+2
-2
Modified
XFE各类拓展.NetCore.Analyzer/ProfilePropertyAutoGenerator.cs
+48
-43
XFEstudio/XFEExtension
现在,可以自动生成配置文件字段对应的属性
9d0626e
代码差异
4 个文件
+61
-56
@@ -0,0 +1,11 @@
1
using XFE各类拓展.NetCore.ProfileExtension;
2
3
namespace XFE各类拓展.NetCore.Analyzer.Test;
4
5
public static partial class SystemProfile
6
{
7
[ProfileProperty]
8
private static string name = string.Empty;
9
[ProfileProperty]
10
private static int _age;
11
}
@@ -1,11 +0,0 @@
1
using XFE各类拓展.NetCore.ImplExtension;
2
3
namespace XFE各类拓展.NetCore.Analyzer.Test;
4
5
6
[CreateImpl]
7
public abstract class Animal(string name, int age)
8
{
9
public string Name { get; set; } = name;
10
public int Age { get; set; } = age;
11
}
@@ -4,6 +4,6 @@ internal class TestClass
4
4
{
5
5
static void Main(string[] args)
6
6
{
7
7
8
8
}
9
}
9
}
@@ -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.Collections.Generic;
4
5
using System.Linq;
5
6
6
7
namespace XFE各类拓展.NetCore.Analyzer
@@ -19,7 +20,7 @@ namespace XFE各类拓展.NetCore.Analyzer
19
20
{
20
21
var root = syntaxTree.GetRoot();
21
22
var classDeclarations = root.DescendantNodes().OfType<ClassDeclarationSyntax>()
22
.Where(classDeclaration => classDeclaration.AttributeLists.Any(IsProfilePropertyAttribute));
23
.Where(classDeclaration => classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword));
23
24
var usingDirectives = root.DescendantNodes().OfType<UsingDirectiveSyntax>().ToArray();
24
25
FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax = null;
25
26
var namespaceResults = root.DescendantNodes().OfType<FileScopedNamespaceDeclarationSyntax>();
@@ -27,72 +28,76 @@ namespace XFE各类拓展.NetCore.Analyzer
27
28
fileScopedNamespaceDeclarationSyntax = namespaceResults.First();
28
29
foreach (var classDeclaration in classDeclarations)
29
30
{
31
var fieldDeclarationSyntaxes = classDeclaration.DescendantNodes().OfType<FieldDeclarationSyntax>()
32
.Where(fieldDeclarationSyntax => fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAttribute) && fieldDeclarationSyntax.Modifiers.Any(SyntaxKind.StaticKeyword));
33
if (fieldDeclarationSyntaxes is null || !fieldDeclarationSyntaxes.Any())
34
{
35
continue;
36
}
30
37
var className = classDeclaration.Identifier.ValueText;
31
var implementationSyntaxTree = GenerateImplementationSyntaxTree(classDeclaration, usingDirectives, fileScopedNamespaceDeclarationSyntax);
32
context.AddSource($"{className}Impl.g.cs", implementationSyntaxTree.ToString());
38
var properties = fieldDeclarationSyntaxes.Select(fieldDeclarationSyntax =>
39
{
40
var variableDeclaration = fieldDeclarationSyntax.Declaration.Variables.First();
41
var fieldName = variableDeclaration.Identifier.Text;
42
var propertyName = fieldName[0] == '_' ? fieldName[1].ToString().ToUpper() + fieldName.Substring(2) : fieldName[0].ToString().ToUpper() + fieldName.Substring(1);
43
var propertyType = fieldDeclarationSyntax.Declaration.Type;
44
var property = SyntaxFactory.PropertyDeclaration(propertyType, propertyName)
45
.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
46
.WithAccessorList(SyntaxFactory.AccessorList(
47
SyntaxFactory.List(new[]
48
{
49
SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
50
.WithBody(SyntaxFactory.Block(SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(fieldName)))),
51
SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
52
.WithBody(
53
SyntaxFactory.Block(
54
SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"{fieldName} = value")),
55
SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"_ = global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.SaveProfile(typeof({className}))"))
56
))
57
})));
58
return property;
59
});
60
var profileClassSyntaxTree = GenerateProfileClassSyntaxTree(classDeclaration, usingDirectives, properties, fileScopedNamespaceDeclarationSyntax);
61
context.AddSource($"{className}.g.cs", profileClassSyntaxTree.ToString());
33
62
}
34
63
}
35
64
}
36
65
37
66
private static bool IsProfilePropertyAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfileProperty");
38
67
39
private static SyntaxTree GenerateImplementationSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes, FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax)
68
private static SyntaxTree GenerateProfileClassSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes, IEnumerable<PropertyDeclarationSyntax> propertyDeclarationSyntaxes, FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax)
40
69
{
41
70
var className = classDeclaration.Identifier.ValueText;
42
ClassDeclarationSyntax implementationClass;
43
if (classDeclaration.ParameterList is null)
44
{
45
implementationClass = SyntaxFactory.ClassDeclaration($"{className}Impl")
46
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
47
.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(className)))
48
.AddMembers(classDeclaration.Members.OfType<ConstructorDeclarationSyntax>().Select(constructor =>
49
{
50
return constructor.WithIdentifier(SyntaxFactory.Identifier($"{className}Impl"))
51
.WithInitializer(SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(constructor.ParameterList.Parameters.Select(parameter =>
52
SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier)))))));
53
}).ToArray())
54
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia($@"/// <summary>
55
/// <seealso cref=""{className}Impl""/> 是根据 <seealso cref=""{className}""/> 自动生成的实现类
56
/// </summary>
57
"))
58
.NormalizeWhitespace();
59
}
60
else
61
{
62
implementationClass = SyntaxFactory.ClassDeclaration($"{className}Impl")
63
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
64
.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(className)))
65
.AddMembers(SyntaxFactory.ConstructorDeclaration($"{className}Impl")
66
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
67
.WithBody(SyntaxFactory.Block())
68
.WithParameterList(classDeclaration.ParameterList)
69
.WithInitializer(SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(classDeclaration.ParameterList.Parameters.Select(parameter => SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier))))))))
70
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia($@"/// <summary>
71
/// <seealso cref=""{className}Impl""/> 是根据 <seealso cref=""{className}""/> 自动生成的实现类
72
/// </summary>
73
"))
74
.NormalizeWhitespace();
75
}
71
var summaryText = $@"/// <remarks>
72
/// <code><seealso cref=""{className}""/> 已自动实现以下属性:</code><br/>
73
/// <code>
74
";
75
summaryText += string.Join("<br/>\n", propertyDeclarationSyntaxes.Select(propertyDeclarationSyntax => $"/// ○ <seealso cref=\"{propertyDeclarationSyntax.Identifier}\"/>")) + "\n/// </code><br/>\n/// <code>来自<seealso cref=\"global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile\"/></code>\n/// </remarks>\n";
76
var profileClass = SyntaxFactory.ClassDeclaration(className)
77
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword))
78
.AddMembers(propertyDeclarationSyntaxes.ToArray())
79
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia(summaryText))
80
.NormalizeWhitespace();
76
81
MemberDeclarationSyntax memberDeclaration;
77
82
if (fileScopedNamespaceDeclarationSyntax is null)
78
83
{
79
84
var namespaceDeclaration = classDeclaration.FirstAncestorOrSelf<NamespaceDeclarationSyntax>();
80
85
if (namespaceDeclaration is null)
81
memberDeclaration = implementationClass;
86
memberDeclaration = profileClass;
82
87
else
83
88
memberDeclaration = SyntaxFactory.NamespaceDeclaration(namespaceDeclaration.Name)
84
.AddMembers(implementationClass);
89
.AddMembers(profileClass);
85
90
}
86
91
else
87
92
{
88
93
memberDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(fileScopedNamespaceDeclarationSyntax.Name)
89
.AddMembers(implementationClass);
94
.AddMembers(profileClass);
90
95
}
91
var implementationCompilationUnit = SyntaxFactory.CompilationUnit()
96
var profileClassCompilationUnit = SyntaxFactory.CompilationUnit()
92
97
.AddUsings(usingDirectiveSyntaxes)
93
98
.AddMembers(memberDeclaration)
94
99
.NormalizeWhitespace();
95
return SyntaxFactory.SyntaxTree(implementationCompilationUnit);
100
return SyntaxFactory.SyntaxTree(profileClassCompilationUnit);
96
101
}
97
102
}
98
103
}