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

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

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

XFEstudio/XFEExtension

修复代码不能添加的bug

4223b78
XFE工作室室长 <xfe18827675401@126.com>
提交于

代码差异

4 个文件 +27 -11
Modified XFE各类拓展.NetCore.Analyzer.Test/SystemProfile.cs +1 -0
@@ -2,6 +2,7 @@
2 2
3 3 namespace XFE各类拓展.NetCore.Analyzer.Test;
4 4
5 [AutoLoadProfile]
5 6 public static partial class SystemProfile
6 7 {
7 8 [ProfileProperty]
Modified XFE各类拓展.NetCore.Analyzer/ProfilePropertyAutoGenerator.cs +24 -9
@@ -43,12 +43,23 @@ namespace XFE各类拓展.NetCore.Analyzer
43 43 var variableDeclaration = fieldDeclarationSyntax.Declaration.Variables.First();
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 fieldDeclarationSyntax.AttributeLists.Where(IsProfilePropertyAttribute).SelectMany(attributeList => attributeList.Attributes).ToList().ForEach(attribute =>
47 {
48 if (attribute.ArgumentList is null)
49 {
50 return;
51 }
52 var argument = attribute.ArgumentList.Arguments.First();
53 if (argument.Expression is LiteralExpressionSyntax literalExpressionSyntax)
54 {
55 propertyName = literalExpressionSyntax.Token.ValueText;
56 }
57 });
46 58 var propertyType = fieldDeclarationSyntax.Declaration.Type;
47 59 var getExpressionStatements = new List<StatementSyntax>();
48 60 if (fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAddGetAttribute))
49 61 {
50 //获取每个特性的传参,并将其作为表达式添加到getExpressionStatements中
51 fieldDeclarationSyntax.AttributeLists.Where(IsProfilePropertyAttribute).SelectMany(attributeList => attributeList.Attributes).ToList().ForEach(attribute =>
62 fieldDeclarationSyntax.AttributeLists.Where(IsProfilePropertyAddGetAttribute).SelectMany(attributeList => attributeList.Attributes).ToList().ForEach(attribute =>
52 63 {
53 64 if (attribute.ArgumentList is null)
54 65 {
@@ -57,7 +68,7 @@ namespace XFE各类拓展.NetCore.Analyzer
57 68 var argument = attribute.ArgumentList.Arguments.First();
58 69 if (argument.Expression is LiteralExpressionSyntax literalExpressionSyntax)
59 70 {
60 getExpressionStatements.Add(SyntaxFactory.ParseStatement(literalExpressionSyntax.Token.ValueText));
71 getExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression(literalExpressionSyntax.Token.ValueText)));
61 72 }
62 73 });
63 74 }
@@ -68,8 +79,7 @@ namespace XFE各类拓展.NetCore.Analyzer
68 79 var setExpressionStatements = new List<StatementSyntax>();
69 80 if (fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAddSetAttribute))
70 81 {
71 //获取每个特性的传参,并将其作为表达式添加到setExpressionStatements中
72 fieldDeclarationSyntax.AttributeLists.Where(IsProfilePropertyAttribute).SelectMany(attributeList => attributeList.Attributes).ToList().ForEach(attribute =>
82 fieldDeclarationSyntax.AttributeLists.Where(IsProfilePropertyAddSetAttribute).SelectMany(attributeList => attributeList.Attributes).ToList().ForEach(attribute =>
73 83 {
74 84 if (attribute.ArgumentList is null)
75 85 {
@@ -78,15 +88,15 @@ namespace XFE各类拓展.NetCore.Analyzer
78 88 var argument = attribute.ArgumentList.Arguments.First();
79 89 if (argument.Expression is LiteralExpressionSyntax literalExpressionSyntax)
80 90 {
81 setExpressionStatements.Add(SyntaxFactory.ParseStatement(literalExpressionSyntax.Token.ValueText));
91 setExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression(literalExpressionSyntax.Token.ValueText)));
82 92 }
83 93 });
84 94 }
85 95 else
86 96 {
87 97 setExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"{fieldName} = value")));
88 setExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"_ = global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.LoadProfile(typeof({className}))")));
89 98 }
99 setExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"_ = global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.LoadProfiles(typeof({className}))")));
90 100 var property = SyntaxFactory.PropertyDeclaration(propertyType, propertyName)
91 101 .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
92 102 .AddAttributeLists(attributeSyntax)
@@ -98,7 +108,7 @@ namespace XFE各类拓展.NetCore.Analyzer
98 108 SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
99 109 .WithBody(SyntaxFactory.Block(setExpressionStatements))
100 110 })));
101 return property;
111 return property.NormalizeWhitespace();
102 112 });
103 113 var profileClassSyntaxTree = GenerateProfileClassSyntaxTree(classDeclaration, usingDirectives, properties, fileScopedNamespaceDeclarationSyntax);
104 114 context.AddSource($"{className}.g.cs", profileClassSyntaxTree.ToString());
@@ -124,7 +134,8 @@ namespace XFE各类拓展.NetCore.Analyzer
124 134 var staticConstructorSyntax = SyntaxFactory.ConstructorDeclaration(className)
125 135 .AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword))
126 136 .WithBody(SyntaxFactory.Block(
127 SyntaxFactory.ParseStatement($"global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.LoadProfile(typeof({className}))")));
137 SyntaxFactory.ParseStatement($"_ = global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.LoadProfiles(typeof({className}));")))
138 .NormalizeWhitespace();
128 139 if (classDeclaration.AttributeLists.Any(IsAutoLoadProfileAttribute))
129 140 {
130 141 var autoLoadProfileAttribute = classDeclaration.AttributeLists.First(attributeList => IsAutoLoadProfileAttribute(attributeList)).Attributes.First();
@@ -136,6 +147,10 @@ namespace XFE各类拓展.NetCore.Analyzer
136 147 memberDeclarations.Add(staticConstructorSyntax);
137 148 }
138 149 }
150 else
151 {
152 memberDeclarations.Add(staticConstructorSyntax);
153 }
139 154 }
140 155 else
141 156 {
Modified XFE各类拓展.NetCore/ProfileExtension/ProfilePropertyAddGetAttribute.cs +1 -1
@@ -4,7 +4,7 @@
4 4 /// 向属性的Get方法中添加代码
5 5 /// </summary>
6 6 /// <param name="funcString">需要添加的代码</param>
7 [AttributeUsage(AttributeTargets.Field)]
7 [AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
8 8 public class ProfilePropertyAddGetAttribute(string funcString) : Attribute
9 9 {
10 10 /// <summary>
Modified XFE各类拓展.NetCore/ProfileExtension/ProfilePropertyAddSetAttribute.cs +1 -1
@@ -4,7 +4,7 @@
4 4 /// 向属性的Set方法中添加代码
5 5 /// </summary>
6 6 /// <param name="funcString">需要添加的代码</param>
7 [AttributeUsage(AttributeTargets.Field)]
7 [AttributeUsage(AttributeTargets.Field,AllowMultiple = true)]
8 8 public class ProfilePropertyAddSetAttribute(string funcString) : Attribute
9 9 {
10 10 /// <summary>