XFEExtension
【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等
关注
0
Fork
0
Star
0
返回提交历史
Modified
XFE各类拓展.NetCore.Analyzer/ProfilePropertyAutoGenerator.cs
+71
-7
Added
XFE各类拓展.NetCore/ProfileExtension/AutoLoadProfileAttribute.cs
+14
-0
Added
XFE各类拓展.NetCore/ProfileExtension/ProfilePropertyAddGetAttribute.cs
+14
-0
Added
XFE各类拓展.NetCore/ProfileExtension/ProfilePropertyAddSetAttribute.cs
+14
-0
XFEstudio/XFEExtension
添加自动加载特性,添加配置属性的GetSet添加方法
661d09c
代码差异
4 个文件
+113
-7
@@ -44,6 +44,49 @@ namespace XFE各类拓展.NetCore.Analyzer
44
44
var fieldName = variableDeclaration.Identifier.Text;
45
45
var propertyName = fieldName[0] == '_' ? fieldName[1].ToString().ToUpper() + fieldName.Substring(2) : fieldName[0].ToString().ToUpper() + fieldName.Substring(1);
46
46
var propertyType = fieldDeclarationSyntax.Declaration.Type;
47
var getExpressionStatements = new List<StatementSyntax>();
48
if (fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAddGetAttribute))
49
{
50
//获取每个特性的传参,并将其作为表达式添加到getExpressionStatements中
51
fieldDeclarationSyntax.AttributeLists.Where(IsProfilePropertyAttribute).SelectMany(attributeList => attributeList.Attributes).ToList().ForEach(attribute =>
52
{
53
if (attribute.ArgumentList is null)
54
{
55
return;
56
}
57
var argument = attribute.ArgumentList.Arguments.First();
58
if (argument.Expression is LiteralExpressionSyntax literalExpressionSyntax)
59
{
60
getExpressionStatements.Add(SyntaxFactory.ParseStatement(literalExpressionSyntax.Token.ValueText));
61
}
62
});
63
}
64
else
65
{
66
getExpressionStatements.Add(SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(fieldName)));
67
}
68
var setExpressionStatements = new List<StatementSyntax>();
69
if (fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAddSetAttribute))
70
{
71
//获取每个特性的传参,并将其作为表达式添加到setExpressionStatements中
72
fieldDeclarationSyntax.AttributeLists.Where(IsProfilePropertyAttribute).SelectMany(attributeList => attributeList.Attributes).ToList().ForEach(attribute =>
73
{
74
if (attribute.ArgumentList is null)
75
{
76
return;
77
}
78
var argument = attribute.ArgumentList.Arguments.First();
79
if (argument.Expression is LiteralExpressionSyntax literalExpressionSyntax)
80
{
81
setExpressionStatements.Add(SyntaxFactory.ParseStatement(literalExpressionSyntax.Token.ValueText));
82
}
83
});
84
}
85
else
86
{
87
setExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"{fieldName} = value")));
88
setExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"_ = global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.LoadProfile(typeof({className}))")));
89
}
47
90
var property = SyntaxFactory.PropertyDeclaration(propertyType, propertyName)
48
91
.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
49
92
.AddAttributeLists(attributeSyntax)
@@ -51,13 +94,9 @@ namespace XFE各类拓展.NetCore.Analyzer
51
94
SyntaxFactory.List(new[]
52
95
{
53
96
SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
54
.WithBody(SyntaxFactory.Block(SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName(fieldName)))),
97
.WithBody(SyntaxFactory.Block(getExpressionStatements)),
55
98
SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
56
.WithBody(
57
SyntaxFactory.Block(
58
SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"{fieldName} = value")),
59
SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"_ = global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.SaveProfile(typeof({className}))"))
60
))
99
.WithBody(SyntaxFactory.Block(setExpressionStatements))
61
100
})));
62
101
return property;
63
102
});
@@ -68,6 +107,9 @@ namespace XFE各类拓展.NetCore.Analyzer
68
107
}
69
108
70
109
private static bool IsProfilePropertyAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfileProperty");
110
private static bool IsAutoLoadProfileAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "AutoLoadProfile");
111
private static bool IsProfilePropertyAddGetAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfilePropertyAddGet");
112
private static bool IsProfilePropertyAddSetAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfilePropertyAddSet");
71
113
72
114
private static SyntaxTree GenerateProfileClassSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes, IEnumerable<PropertyDeclarationSyntax> propertyDeclarationSyntaxes, FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax)
73
115
{
@@ -77,9 +119,31 @@ namespace XFE各类拓展.NetCore.Analyzer
77
119
/// <code>
78
120
";
79
121
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";
122
var memberDeclarations = new List<MemberDeclarationSyntax>();
123
memberDeclarations.AddRange(propertyDeclarationSyntaxes);
124
var staticConstructorSyntax = SyntaxFactory.ConstructorDeclaration(className)
125
.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword))
126
.WithBody(SyntaxFactory.Block(
127
SyntaxFactory.ParseStatement($"global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.LoadProfile(typeof({className}))")));
128
if (classDeclaration.AttributeLists.Any(IsAutoLoadProfileAttribute))
129
{
130
var autoLoadProfileAttribute = classDeclaration.AttributeLists.First(attributeList => IsAutoLoadProfileAttribute(attributeList)).Attributes.First();
131
if (autoLoadProfileAttribute.ArgumentList != null)
132
{
133
var argument = autoLoadProfileAttribute.ArgumentList.Arguments.First();
134
if (argument.Expression is LiteralExpressionSyntax literalExpressionSyntax && literalExpressionSyntax.Token.ValueText == "true")
135
{
136
memberDeclarations.Add(staticConstructorSyntax);
137
}
138
}
139
}
140
else
141
{
142
memberDeclarations.Add(staticConstructorSyntax);
143
}
80
144
var profileClass = SyntaxFactory.ClassDeclaration(className)
81
145
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword))
82
.AddMembers(propertyDeclarationSyntaxes.ToArray())
146
.AddMembers(memberDeclarations.ToArray())
83
147
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia(summaryText))
84
148
.NormalizeWhitespace();
85
149
MemberDeclarationSyntax memberDeclaration;
@@ -0,0 +1,14 @@
1
namespace XFE各类拓展.NetCore.ProfileExtension;
2
3
/// <summary>
4
/// 自动加载配置文件
5
/// </summary>
6
/// <param name="autoLoad">是否自动加载</param>
7
[AttributeUsage(AttributeTargets.Class)]
8
public class AutoLoadProfileAttribute(bool autoLoad = true) : Attribute
9
{
10
/// <summary>
11
/// 是否自动加载
12
/// </summary>
13
public bool AutoLoad { get; set; } = autoLoad;
14
}
@@ -0,0 +1,14 @@
1
namespace XFE各类拓展.NetCore.ProfileExtension;
2
3
/// <summary>
4
/// 向属性的Get方法中添加代码
5
/// </summary>
6
/// <param name="funcString">需要添加的代码</param>
7
[AttributeUsage(AttributeTargets.Field)]
8
public class ProfilePropertyAddGetAttribute(string funcString) : Attribute
9
{
10
/// <summary>
11
/// 需要添加的代码
12
/// </summary>
13
public string FuncString { get; set; } = funcString;
14
}
@@ -0,0 +1,14 @@
1
namespace XFE各类拓展.NetCore.ProfileExtension;
2
3
/// <summary>
4
/// 向属性的Set方法中添加代码
5
/// </summary>
6
/// <param name="funcString">需要添加的代码</param>
7
[AttributeUsage(AttributeTargets.Field)]
8
public class ProfilePropertyAddSetAttribute(string funcString) : Attribute
9
{
10
/// <summary>
11
/// 需要添加的代码
12
/// </summary>
13
public string FuncString { get; set; } = funcString;
14
}