XFEExtension
【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等
关注
0
Fork
0
Star
0
返回提交历史
Added
XFE各类拓展.NetCore.Analyzer/AnalyzerReleases.Shipped.md
+8
-0
Added
XFE各类拓展.NetCore.Analyzer/AnalyzerReleases.Unshipped.md
+6
-0
Added
XFE各类拓展.NetCore.Analyzer/Diagnostics/ProfileExtensionDiagnostics.cs
+80
-0
Renamed
XFE各类拓展.NetCore.Analyzer/Generator/ImplementAutoGenerator.cs
+1
-1
Renamed
XFE各类拓展.NetCore.Analyzer/Generator/ProfilePropertyAutoGenerator.cs
+54
-16
Modified
XFE各类拓展.NetCore/ProfileExtension/XFEProfile.cs
+1
-2
XFEstudio/XFEExtension
添加特性的诊断
064f056
代码差异
6 个文件
+150
-19
@@ -0,0 +1,8 @@
1
## Release 1.7.6
2
3
### New Rules
4
5
Rule ID | Category | Severity | Notes
6
--------|----------|----------|-------
7
XFE0002 | XFE各类拓展.NetCore.Analyzer.Diagnostics | Error | ProfileExtensionDiagnostics
8
XFW0003 | XFE各类拓展.NetCore.Analyzer.Diagnostics | Warning | ProfileExtensionDiagnostics
@@ -0,0 +1,6 @@
1
### New Rules
2
3
Rule ID | Category | Severity | Notes
4
--------|----------|----------|-------
5
XFE0002 | XFE各类拓展.NetCore.Analyzer.Diagnostics | Error | ProfileExtensionDiagnostics
6
XFW0003 | XFE各类拓展.NetCore.Analyzer.Diagnostics | Warning | ProfileExtensionDiagnostics
@@ -0,0 +1,80 @@
1
using Microsoft.CodeAnalysis;
2
using Microsoft.CodeAnalysis.CSharp.Syntax;
3
using System.Text.RegularExpressions;
4
using XFE各类拓展.NetCore.Analyzer.Generator;
5
6
namespace XFE各类拓展.NetCore.Analyzer.Diagnostics
7
{
8
[Generator]
9
public class ProfileExtensionDiagnostics : ISourceGenerator
10
{
11
public static readonly DiagnosticDescriptor AddGetNoResultError = new DiagnosticDescriptor("XFE0002",
12
"Get方法没有返回值",
13
"设置了自定义的Get方法但是没有返回值:'{0}'",
14
"XFE各类拓展.NetCore.Analyzer.Diagnostics",
15
DiagnosticSeverity.Error,
16
true,
17
"设置了自定义的Get方法但是没有返回值.",
18
null);
19
public static readonly DiagnosticDescriptor AddSetNoSetResultWarning = new DiagnosticDescriptor("XFW0003",
20
"Set方法没有设置值",
21
"设置了自定义的Set方法但是没有设置实际的字段对应的值:'{0}'",
22
"XFE各类拓展.NetCore.Analyzer.Diagnostics",
23
DiagnosticSeverity.Warning,
24
true,
25
"设置了自定义的Set方法但是没有设置实际的字段对应的值.",
26
);
27
public void Initialize(GeneratorInitializationContext context)
28
{
29
}
30
public void Execute(GeneratorExecutionContext context)
31
{
32
foreach (var syntaxTree in context.Compilation.SyntaxTrees)
33
{
34
var root = syntaxTree.GetRoot();
35
foreach (var classDeclaration in ProfilePropertyAutoGenerator.GetClassDeclarations(root))
36
{
37
foreach (var fieldDeclaration in ProfilePropertyAutoGenerator.GetFieldDeclarations(classDeclaration))
38
{
39
var getAttributeHasResult = false;
40
var setAttributeSetResult = false;
41
foreach (var attributeSyntax in ProfilePropertyAutoGenerator.GetProfilePropertyAddGetAttributeList(fieldDeclaration))
42
{
43
if (attributeSyntax.ArgumentList is null)
44
{
45
continue;
46
}
47
var argument = attributeSyntax.ArgumentList.Arguments.First();
48
if (argument.Expression is LiteralExpressionSyntax literalExpressionSyntax && literalExpressionSyntax.Token.ValueText.Contains("return"))
49
{
50
getAttributeHasResult = true;
51
}
52
}
53
foreach (var attributeSyntax in ProfilePropertyAutoGenerator.GetProfilePropertyAddSetAttributeList(fieldDeclaration))
54
{
55
if (attributeSyntax.ArgumentList is null)
56
{
57
continue;
58
}
59
var argument = attributeSyntax.ArgumentList.Arguments.First();
60
if (argument.Expression is LiteralExpressionSyntax literalExpressionSyntax && Regex.IsMatch(literalExpressionSyntax.Token.ValueText, $@"{fieldDeclaration.Declaration.Variables.First().Identifier.ValueText}\s*=\s*value"))
61
{
62
setAttributeSetResult = true;
63
}
64
}
65
if (!getAttributeHasResult)
66
{
67
var diagnostic = Diagnostic.Create(AddGetNoResultError, fieldDeclaration.GetLocation(), fieldDeclaration.Declaration.Variables.First().Identifier.ValueText);
68
context.ReportDiagnostic(diagnostic);
69
}
70
if (!setAttributeSetResult)
71
{
72
var diagnostic = Diagnostic.Create(AddSetNoSetResultWarning, fieldDeclaration.GetLocation(), fieldDeclaration.Declaration.Variables.First().Identifier.ValueText);
73
context.ReportDiagnostic(diagnostic);
74
}
75
}
76
}
77
}
78
}
79
}
80
}
@@ -3,7 +3,7 @@ using Microsoft.CodeAnalysis.CSharp;
3
3
using Microsoft.CodeAnalysis.CSharp.Syntax;
4
4
using System.Linq;
5
5
6
namespace XFE各类拓展.NetCore.Analyzer
6
namespace XFE各类拓展.NetCore.Analyzer.Generator
7
7
{
8
8
[Generator]
9
9
public class ImplementAutoGenerator : ISourceGenerator
@@ -4,7 +4,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
4
4
using System.Collections.Generic;
5
5
using System.Linq;
6
6
7
namespace XFE各类拓展.NetCore.Analyzer
7
namespace XFE各类拓展.NetCore.Analyzer.Generator
8
8
{
9
9
[Generator]
10
10
public class ProfilePropertyAutoGenerator : ISourceGenerator
@@ -19,17 +19,12 @@ namespace XFE各类拓展.NetCore.Analyzer
19
19
foreach (var syntaxTree in syntaxTrees)
20
20
{
21
21
var root = syntaxTree.GetRoot();
22
var classDeclarations = root.DescendantNodes().OfType<ClassDeclarationSyntax>()
23
.Where(classDeclaration => classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword));
22
var classDeclarations = GetClassDeclarations(root);
24
23
var usingDirectives = root.DescendantNodes().OfType<UsingDirectiveSyntax>().ToArray();
25
FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax = null;
26
var namespaceResults = root.DescendantNodes().OfType<FileScopedNamespaceDeclarationSyntax>();
27
if (namespaceResults != null && namespaceResults.Count() > 0)
28
fileScopedNamespaceDeclarationSyntax = namespaceResults.First();
24
var fileScopedNamespaceDeclarationSyntax = GetFileScopedNamespaceDeclaration(root);
29
25
foreach (var classDeclaration in classDeclarations)
30
26
{
31
var fieldDeclarationSyntaxes = classDeclaration.DescendantNodes().OfType<FieldDeclarationSyntax>()
32
.Where(fieldDeclarationSyntax => fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAttribute) && fieldDeclarationSyntax.Modifiers.Any(SyntaxKind.StaticKeyword));
27
var fieldDeclarationSyntaxes = GetFieldDeclarations(classDeclaration);
33
28
if (fieldDeclarationSyntaxes is null || !fieldDeclarationSyntaxes.Any())
34
29
{
35
30
continue;
@@ -43,7 +38,7 @@ namespace XFE各类拓展.NetCore.Analyzer
43
38
var variableDeclaration = fieldDeclarationSyntax.Declaration.Variables.First();
44
39
var fieldName = variableDeclaration.Identifier.Text;
45
40
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 =>
41
GetProfilePropertyAttributeList(fieldDeclarationSyntax).ForEach(attribute =>
47
42
{
48
43
if (attribute.ArgumentList is null)
49
44
{
@@ -59,7 +54,7 @@ namespace XFE各类拓展.NetCore.Analyzer
59
54
var getExpressionStatements = new List<StatementSyntax>();
60
55
if (fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAddGetAttribute))
61
56
{
62
fieldDeclarationSyntax.AttributeLists.Where(IsProfilePropertyAddGetAttribute).SelectMany(attributeList => attributeList.Attributes).ToList().ForEach(attribute =>
57
GetProfilePropertyAddGetAttributeList(fieldDeclarationSyntax).ForEach(attribute =>
63
58
{
64
59
if (attribute.ArgumentList is null)
65
60
{
@@ -79,7 +74,7 @@ namespace XFE各类拓展.NetCore.Analyzer
79
74
var setExpressionStatements = new List<StatementSyntax>();
80
75
if (fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAddSetAttribute))
81
76
{
82
fieldDeclarationSyntax.AttributeLists.Where(IsProfilePropertyAddSetAttribute).SelectMany(attributeList => attributeList.Attributes).ToList().ForEach(attribute =>
77
GetProfilePropertyAddSetAttributeList(fieldDeclarationSyntax).ForEach(attribute =>
83
78
{
84
79
if (attribute.ArgumentList is null)
85
80
{
@@ -117,10 +112,53 @@ namespace XFE各类拓展.NetCore.Analyzer
117
112
}
118
113
}
119
114
120
private static bool IsProfilePropertyAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfileProperty");
121
private static bool IsAutoLoadProfileAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "AutoLoadProfile");
122
private static bool IsProfilePropertyAddGetAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfilePropertyAddGet");
123
private static bool IsProfilePropertyAddSetAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfilePropertyAddSet");
115
public static bool IsProfilePropertyAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfileProperty");
116
117
public static List<AttributeSyntax> GetProfilePropertyAttributeList(FieldDeclarationSyntax fieldDeclaration)
118
{
119
return fieldDeclaration.AttributeLists.Where(IsProfilePropertyAttribute).SelectMany(attributeList => attributeList.Attributes).ToList();
120
}
121
122
public static bool IsAutoLoadProfileAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "AutoLoadProfile");
123
124
public static List<AttributeSyntax> GetAutoLoadProfileAttribute(FieldDeclarationSyntax fieldDeclaration)
125
{
126
return fieldDeclaration.AttributeLists.Where(IsAutoLoadProfileAttribute).SelectMany(attributeList => attributeList.Attributes).ToList();
127
}
128
129
public static bool IsProfilePropertyAddGetAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfilePropertyAddGet");
130
131
public static List<AttributeSyntax> GetProfilePropertyAddGetAttributeList(FieldDeclarationSyntax fieldDeclaration)
132
{
133
return fieldDeclaration.AttributeLists.Where(IsProfilePropertyAddGetAttribute).SelectMany(attributeList => attributeList.Attributes).ToList();
134
}
135
136
public static bool IsProfilePropertyAddSetAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "ProfilePropertyAddSet");
137
138
public static List<AttributeSyntax> GetProfilePropertyAddSetAttributeList(FieldDeclarationSyntax fieldDeclaration)
139
{
140
return fieldDeclaration.AttributeLists.Where(IsProfilePropertyAddSetAttribute).SelectMany(attributeList => attributeList.Attributes).ToList();
141
}
142
143
public static FileScopedNamespaceDeclarationSyntax GetFileScopedNamespaceDeclaration(SyntaxNode rootNode)
144
{
145
var namespaceResults = rootNode.DescendantNodes().OfType<FileScopedNamespaceDeclarationSyntax>();
146
if (namespaceResults != null && namespaceResults.Count() > 0)
147
return namespaceResults.First();
148
return null;
149
}
150
151
public static IEnumerable<FieldDeclarationSyntax> GetFieldDeclarations(ClassDeclarationSyntax classDeclaration)
152
{
153
return classDeclaration.DescendantNodes().OfType<FieldDeclarationSyntax>()
154
.Where(fieldDeclarationSyntax => fieldDeclarationSyntax.AttributeLists.Any(IsProfilePropertyAttribute) && fieldDeclarationSyntax.Modifiers.Any(SyntaxKind.StaticKeyword));
155
}
156
157
public static IEnumerable<ClassDeclarationSyntax> GetClassDeclarations(SyntaxNode rootNode)
158
{
159
return rootNode.DescendantNodes().OfType<ClassDeclarationSyntax>()
160
.Where(classDeclaration => classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword));
161
}
124
162
125
163
private static SyntaxTree GenerateProfileClassSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes, IEnumerable<PropertyDeclarationSyntax> propertyDeclarationSyntaxes, FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax)
126
164
{
@@ -1,5 +1,4 @@
1
using System.Reflection;
2
using XFE各类拓展.NetCore.FormatExtension;
1
using XFE各类拓展.NetCore.FormatExtension;
3
2
4
3
namespace XFE各类拓展.NetCore.ProfileExtension;
5
4