XFEExtension
【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等
关注
0
Fork
0
Star
0
返回提交历史
Modified
XFE各类拓展.NetCore.Analyzer.Test/TestAbstractClass.cs
+8
-9
Added
XFE各类拓展.NetCore.Analyzer.Test/TestClass.cs
+9
-0
Modified
XFE各类拓展.NetCore.Analyzer.Test/XFE各类拓展.NetCore.Analyzer.Test.csproj
+1
-0
Modified
XFE各类拓展.NetCore.Analyzer/ImplementAutoGenerator.cs
+18
-6
Modified
XFE各类拓展.NetCore/XFE各类拓展.NetCore.csproj
+1
-1
XFEstudio/XFEExtension
现在ImplementAutoGenerator能够正常生成文件范围声明的命名空间所包含的类
2b9c6c8
代码差异
5 个文件
+37
-16
@@ -1,12 +1,11 @@
1
1
using XFE各类拓展.NetCore.ImplExtension;
2
2
3
namespace XFE各类拓展.NetCore.Analyzer.Test
3
namespace XFE各类拓展.NetCore.Analyzer.Test;
4
5
6
[CreateImpl]
7
public abstract class Animal(string name, int age)
4
8
{
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
}
12
}
9
public string Name { get; set; } = name;
10
public int Age { get; set; } = age;
11
}
@@ -0,0 +1,9 @@
1
namespace XFE各类拓展.NetCore.Analyzer.Test;
2
3
internal class TestClass
4
{
5
static void Main(string[] args)
6
{
7
8
}
9
}
@@ -4,6 +4,7 @@
4
4
<TargetFramework>net8.0</TargetFramework>
5
5
<ImplicitUsings>enable</ImplicitUsings>
6
6
<Nullable>enable</Nullable>
7
<OutputType>Exe</OutputType>
7
8
</PropertyGroup>
8
9
9
10
<ItemGroup>
@@ -21,10 +21,14 @@ namespace XFE各类拓展.NetCore.Analyzer
21
21
var classDeclarations = root.DescendantNodes().OfType<ClassDeclarationSyntax>()
22
22
.Where(classDeclaration => classDeclaration.AttributeLists.Any(IsCreateImplAttribute));
23
23
var usingDirectives = root.DescendantNodes().OfType<UsingDirectiveSyntax>().ToArray();
24
FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax = null;
25
var namespaceResults = root.DescendantNodes().OfType<FileScopedNamespaceDeclarationSyntax>();
26
if (namespaceResults != null && namespaceResults.Count() > 0)
27
fileScopedNamespaceDeclarationSyntax = namespaceResults.First();
24
28
foreach (var classDeclaration in classDeclarations)
25
29
{
26
30
var className = classDeclaration.Identifier.ValueText;
27
var implementationSyntaxTree = GenerateImplementationSyntaxTree(classDeclaration, usingDirectives);
31
var implementationSyntaxTree = GenerateImplementationSyntaxTree(classDeclaration, usingDirectives, fileScopedNamespaceDeclarationSyntax);
28
32
context.AddSource($"{className}Impl.g.cs", implementationSyntaxTree.ToString());
29
33
}
30
34
}
@@ -32,7 +36,7 @@ namespace XFE各类拓展.NetCore.Analyzer
32
36
33
37
private static bool IsCreateImplAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "CreateImpl");
34
38
35
private static SyntaxTree GenerateImplementationSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes)
39
private static SyntaxTree GenerateImplementationSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes, FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclarationSyntax)
36
40
{
37
41
var className = classDeclaration.Identifier.ValueText;
38
42
ClassDeclarationSyntax implementationClass;
@@ -69,13 +73,21 @@ namespace XFE各类拓展.NetCore.Analyzer
69
73
"))
70
74
.NormalizeWhitespace();
71
75
}
72
var namespaceDeclaration = classDeclaration.FirstAncestorOrSelf<NamespaceDeclarationSyntax>();
73
76
MemberDeclarationSyntax memberDeclaration;
74
if (namespaceDeclaration is null)
75
memberDeclaration = implementationClass;
77
if (fileScopedNamespaceDeclarationSyntax is null)
78
{
79
var namespaceDeclaration = classDeclaration.FirstAncestorOrSelf<NamespaceDeclarationSyntax>();
80
if (namespaceDeclaration is null)
81
memberDeclaration = implementationClass;
82
else
83
memberDeclaration = SyntaxFactory.NamespaceDeclaration(namespaceDeclaration.Name)
84
.AddMembers(implementationClass);
85
}
76
86
else
77
memberDeclaration = SyntaxFactory.NamespaceDeclaration(namespaceDeclaration.Name)
87
{
88
memberDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(fileScopedNamespaceDeclarationSyntax.Name)
78
89
.AddMembers(implementationClass);
90
}
79
91
var implementationCompilationUnit = SyntaxFactory.CompilationUnit()
80
92
.AddUsings(usingDirectiveSyntaxes)
81
93
.AddMembers(memberDeclaration)
@@ -63,7 +63,7 @@
63
63
<Pack>True</Pack>
64
64
<PackagePath>\</PackagePath>
65
65
</None>
66
<None Include="C:\Users\XFEstudio\Desktop\work\C#\GitHub\XFEstudio\XFE各类拓展\XFE各类拓展.NetCore.Analyzer\bin\Release\netstandard2.0\XFE各类拓展.NetCore.Analyzer.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
66
<None Include="..\XFE各类拓展.NetCore.Analyzer\bin\Release\netstandard2.0\XFE各类拓展.NetCore.Analyzer.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
67
67
</ItemGroup>
68
68
69
69
<ItemGroup>