XFEExtension
【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等
关注
0
Fork
0
Star
0
返回提交历史
Modified
README.md
+12
-0
Added
XFE各类拓展.NetCore.Analyzer.Test/TestAbstractClass.cs
+12
-0
Added
XFE各类拓展.NetCore.Analyzer.Test/XFE各类拓展.NetCore.Analyzer.Test.csproj
+14
-0
Added
XFE各类拓展.NetCore.Analyzer/ImplementAutoGenerator.cs
+94
-0
Added
XFE各类拓展.NetCore.Analyzer/XFE各类拓展.NetCore.Analyzer.csproj
+19
-0
Added
XFE各类拓展.NetCore/ImplExtension/CreateImpl.cs
+9
-0
Modified
XFE各类拓展.NetCore/XFE各类拓展.NetCore.csproj
+6
-5
Modified
XFE各类拓展.sln
+16
-1
XFEstudio/XFEExtension
现在可以自动生成类的默认实现类,加快编写速度
27c8527
代码差异
8 个文件
+182
-6
@@ -103,6 +103,18 @@ memorableXFEChatGPT.AskChatGPT("新的对话ID", Guid.NewGuid().ToString(), askC
103
103
104
104
---
105
105
106
## 自动生成实现类
107
108
```csharp
109
[CreateImpl]
110
abstract class TestAbstractClass
111
{
112
public abstract void TestMethod();
113
}
114
115
class
116
---
117
106
118
## IO流拓展操作示例
107
119
108
120
```csharp
@@ -0,0 +1,12 @@
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
}
12
}
@@ -0,0 +1,14 @@
1
<Project Sdk="Microsoft.NET.Sdk">
2
3
<PropertyGroup>
4
<TargetFramework>net8.0</TargetFramework>
5
<ImplicitUsings>enable</ImplicitUsings>
6
<Nullable>enable</Nullable>
7
</PropertyGroup>
8
9
<ItemGroup>
10
<ProjectReference Include="..\XFE各类拓展.NetCore.Analyzer\XFE各类拓展.NetCore.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
11
<ProjectReference Include="..\XFE各类拓展.NetCore\XFE各类拓展.NetCore.csproj" />
12
</ItemGroup>
13
14
</Project>
@@ -0,0 +1,94 @@
1
using Microsoft.CodeAnalysis;
2
using Microsoft.CodeAnalysis.CSharp;
3
using Microsoft.CodeAnalysis.CSharp.Syntax;
4
using System.Linq;
5
6
namespace XFE各类拓展.NetCore.Analyzer
7
{
8
[Generator]
9
public class ImplementAutoGenerator : ISourceGenerator
10
{
11
public void Initialize(GeneratorInitializationContext context)
12
{
13
}
14
15
public void Execute(GeneratorExecutionContext context)
16
{
17
var syntaxTrees = context.Compilation.SyntaxTrees;
18
foreach (var syntaxTree in syntaxTrees)
19
{
20
var root = syntaxTree.GetRoot();
21
var classDeclarations = root.DescendantNodes().OfType<ClassDeclarationSyntax>()
22
.Where(classDeclaration => classDeclaration.AttributeLists.Any(IsCreateImplAttribute));
23
var usingDirectives = root.DescendantNodes().OfType<UsingDirectiveSyntax>().ToArray();
24
foreach (var classDeclaration in classDeclarations)
25
{
26
var className = classDeclaration.Identifier.ValueText;
27
// 向类中添加一个空的Test方法
28
var method = SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), "Test")
29
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
30
.WithBody(SyntaxFactory.Block())
31
.NormalizeWhitespace();
32
// 将方法添加到原来的类中
33
var newClass = classDeclaration.AddMembers(method);
34
var implementationSyntaxTree = GenerateImplementationSyntaxTree(classDeclaration, usingDirectives);
35
context.AddSource($"{className}Impl.g.cs", implementationSyntaxTree.ToString());
36
}
37
}
38
}
39
40
private static bool IsCreateImplAttribute(AttributeListSyntax attributeList) => attributeList.Attributes.Any(attribute => attribute.Name.ToString() == "CreateImpl");
41
42
private static SyntaxTree GenerateImplementationSyntaxTree(ClassDeclarationSyntax classDeclaration, UsingDirectiveSyntax[] usingDirectiveSyntaxes)
43
{
44
var className = classDeclaration.Identifier.ValueText;
45
ClassDeclarationSyntax implementationClass;
46
if (classDeclaration.ParameterList is null)
47
{
48
// 添加summary注释
49
implementationClass = SyntaxFactory.ClassDeclaration($"{className}Impl")
50
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
51
.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(className)))
52
.AddMembers(classDeclaration.Members.OfType<ConstructorDeclarationSyntax>().Select(constructor =>
53
{
54
return constructor.WithIdentifier(SyntaxFactory.Identifier($"{className}Impl"))
55
.WithInitializer(SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(constructor.ParameterList.Parameters.Select(parameter =>
56
SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier)))))));
57
}).ToArray())
58
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia($@"/// <summary>
59
/// <seealso cref=""{className}Impl""/> 是根据 <seealso cref=""{className}""/> 自动生成的实现类
60
/// </summary>
61
"))
62
.NormalizeWhitespace();
63
}
64
else
65
{
66
implementationClass = SyntaxFactory.ClassDeclaration($"{className}Impl")
67
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
68
.AddBaseListTypes(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(className)))
69
.AddMembers(SyntaxFactory.ConstructorDeclaration($"{className}Impl")
70
.AddModifiers(SyntaxFactory.Token(SyntaxKind.InternalKeyword))
71
.WithBody(SyntaxFactory.Block())
72
.WithParameterList(classDeclaration.ParameterList)
73
.WithInitializer(SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(classDeclaration.ParameterList.Parameters.Select(parameter => SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameter.Identifier))))))))
74
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia($@"/// <summary>
75
/// <seealso cref=""{className}Impl""/> 是根据 <seealso cref=""{className}""/> 自动生成的实现类
76
/// </summary>
77
"))
78
.NormalizeWhitespace();
79
}
80
var namespaceDeclaration = classDeclaration.FirstAncestorOrSelf<NamespaceDeclarationSyntax>();
81
MemberDeclarationSyntax memberDeclaration;
82
if (namespaceDeclaration is null)
83
memberDeclaration = implementationClass;
84
else
85
memberDeclaration = SyntaxFactory.NamespaceDeclaration(namespaceDeclaration.Name)
86
.AddMembers(implementationClass);
87
var implementationCompilationUnit = SyntaxFactory.CompilationUnit()
88
.AddUsings(usingDirectiveSyntaxes)
89
.AddMembers(memberDeclaration)
90
.NormalizeWhitespace();
91
return SyntaxFactory.SyntaxTree(implementationCompilationUnit);
92
}
93
}
94
}
@@ -0,0 +1,19 @@
1
<Project Sdk="Microsoft.NET.Sdk">
2
3
<PropertyGroup>
4
<TargetFramework>netstandard2.0</TargetFramework>
5
</PropertyGroup>
6
7
<PropertyGroup>
8
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
9
<!-- Generates a package at build -->
10
<IncludeBuildOutput>false</IncludeBuildOutput>
11
<!-- Do not include the generator as a lib dependency -->
12
</PropertyGroup>
13
14
<ItemGroup>
15
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
16
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
17
</ItemGroup>
18
19
</Project>
@@ -0,0 +1,9 @@
1
namespace XFE各类拓展.NetCore.ImplExtension;
2
3
/// <summary>
4
/// 创建一个类的实现类
5
/// </summary>
6
public class CreateImpl : Attribute
7
{
8
9
}
@@ -19,11 +19,11 @@
19
19
<PackageTags>XFE;拓展;GPT;Server;服务器;测试;XFEExtension</PackageTags>
20
20
<PackageReleaseNotes>## 调整
21
21
22
现在,无论是有返回值异步方法还是同步方法,都会显示其返回值
22
无
23
23
24
24
## 新增
25
25
26
无
26
现在可以自动生成类的默认实现类,加快编写速度
27
27
28
28
## 严重
29
29
@@ -32,9 +32,9 @@
32
32
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
33
33
<PackageLicenseFile>LICENSE</PackageLicenseFile>
34
34
<AssemblyOriginatorKeyFile>..\XFEstudio.pfx</AssemblyOriginatorKeyFile>
35
<AssemblyVersion>1.5.15.3</AssemblyVersion>
36
<FileVersion>1.5.15.3</FileVersion>
37
<Version>1.5.3</Version>
35
<AssemblyVersion>1.6.16.0</AssemblyVersion>
36
<FileVersion>1.6.16.0</FileVersion>
37
<Version>1.6.0</Version>
38
38
<Authors>XFEstudio</Authors>
39
39
</PropertyGroup>
40
40
@@ -63,6 +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
67
</ItemGroup>
67
68
68
69
<ItemGroup>
@@ -5,7 +5,14 @@ VisualStudioVersion = 17.6.33723.286
5
5
MinimumVisualStudioVersion = 10.0.40219.1
6
6
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XFE各类拓展", "XFE各类拓展\XFE各类拓展.csproj", "{864ED677-E30C-40C5-BF6A-2012CEC0D8C7}"
7
7
EndProject
8
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XFE各类拓展.NetCore", "XFE各类拓展.NetCore\XFE各类拓展.NetCore.csproj", "{24AF7E8C-69DF-4019-A351-73DE14C8C594}"
8
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XFE各类拓展.NetCore", "XFE各类拓展.NetCore\XFE各类拓展.NetCore.csproj", "{24AF7E8C-69DF-4019-A351-73DE14C8C594}"
9
ProjectSection(ProjectDependencies) = postProject
10
{BF4CBCA6-F808-47BE-8539-A3CBBCE00741} = {BF4CBCA6-F808-47BE-8539-A3CBBCE00741}
11
EndProjectSection
12
EndProject
13
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XFE各类拓展.NetCore.Analyzer", "XFE各类拓展.NetCore.Analyzer\XFE各类拓展.NetCore.Analyzer.csproj", "{BF4CBCA6-F808-47BE-8539-A3CBBCE00741}"
14
EndProject
15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XFE各类拓展.NetCore.Analyzer.Test", "XFE各类拓展.NetCore.Analyzer.Test\XFE各类拓展.NetCore.Analyzer.Test.csproj", "{E0B36AF9-E3E0-47D7-AC06-91FA8998C968}"
9
16
EndProject
10
17
Global
11
18
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +28,14 @@ Global
21
28
{24AF7E8C-69DF-4019-A351-73DE14C8C594}.Debug|Any CPU.Build.0 = Debug|Any CPU
22
29
{24AF7E8C-69DF-4019-A351-73DE14C8C594}.Release|Any CPU.ActiveCfg = Release|Any CPU
23
30
{24AF7E8C-69DF-4019-A351-73DE14C8C594}.Release|Any CPU.Build.0 = Release|Any CPU
31
{BF4CBCA6-F808-47BE-8539-A3CBBCE00741}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32
{BF4CBCA6-F808-47BE-8539-A3CBBCE00741}.Debug|Any CPU.Build.0 = Debug|Any CPU
33
{BF4CBCA6-F808-47BE-8539-A3CBBCE00741}.Release|Any CPU.ActiveCfg = Release|Any CPU
34
{BF4CBCA6-F808-47BE-8539-A3CBBCE00741}.Release|Any CPU.Build.0 = Release|Any CPU
35
{E0B36AF9-E3E0-47D7-AC06-91FA8998C968}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36
{E0B36AF9-E3E0-47D7-AC06-91FA8998C968}.Debug|Any CPU.Build.0 = Debug|Any CPU
37
{E0B36AF9-E3E0-47D7-AC06-91FA8998C968}.Release|Any CPU.ActiveCfg = Release|Any CPU
38
{E0B36AF9-E3E0-47D7-AC06-91FA8998C968}.Release|Any CPU.Build.0 = Release|Any CPU
24
39
EndGlobalSection
25
40
GlobalSection(SolutionProperties) = preSolution
26
41
HideSolutionNode = FALSE