XFEExtension
【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等
关注
0
Fork
0
Star
0
返回提交历史
Modified
XFE各类拓展.NetCore.Analyzer/ProfilePropertyAutoGenerator.cs
+2
-2
Modified
XFE各类拓展.NetCore/ProfileExtension/XFEProfile.cs
+67
-27
XFEstudio/XFEExtension
分离了同步和异步方法,不再合并
68801ec
代码差异
2 个文件
+69
-29
@@ -96,7 +96,7 @@ namespace XFE各类拓展.NetCore.Analyzer
96
96
{
97
97
setExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"{fieldName} = value")));
98
98
}
99
setExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"_ = global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.SaveProfile(typeof({className}))")));
99
setExpressionStatements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.ParseExpression($"global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.SaveProfile(typeof({className}))")));
100
100
var property = SyntaxFactory.PropertyDeclaration(propertyType, propertyName)
101
101
.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
102
102
.AddAttributeLists(attributeSyntax)
@@ -135,7 +135,7 @@ namespace XFE各类拓展.NetCore.Analyzer
135
135
var staticConstructorSyntax = SyntaxFactory.ConstructorDeclaration(className)
136
136
.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword))
137
137
.WithBody(SyntaxFactory.Block(
138
SyntaxFactory.ParseStatement($"global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.LoadProfiles(typeof({className})).Wait();")));
138
SyntaxFactory.ParseStatement($"global::XFE各类拓展.NetCore.ProfileExtension.XFEProfile.LoadProfiles(typeof({className}));")));
139
139
if (classDeclaration.AttributeLists.Any(IsAutoLoadProfileAttribute))
140
140
{
141
141
var autoLoadProfileAttribute = classDeclaration.AttributeLists.First(attributeList => IsAutoLoadProfileAttribute(attributeList)).Attributes.First();
@@ -26,39 +26,72 @@ public abstract class XFEProfile
26
26
/// </summary>
27
27
/// <param name="profileInfo"></param>
28
28
/// <returns></returns>
29
public static async Task LoadProfiles(params ProfileInfo[] profileInfo)
29
public static void LoadProfiles(params ProfileInfo[] profileInfo)
30
30
{
31
await Task.Run(() =>
31
Profiles.AddRange(profileInfo);
32
foreach (var profile in Profiles)
32
33
{
33
Profiles.AddRange(profileInfo);
34
foreach (var profile in Profiles)
34
if (!File.Exists(profile.Path))
35
continue;
36
XFEDictionary propertyFileContent = File.ReadAllText(profile.Path);
37
for (int i = 0; i < profile.PropertiesInfo.Count; i++)
35
38
{
36
if (!File.Exists(profile.Path))
37
continue;
38
XFEDictionary propertyFileContent = File.ReadAllText(profile.Path);
39
for (int i = 0; i < profile.PropertiesInfo.Count; i++)
39
for (int j = 0; j < propertyFileContent.Count; j++)
40
40
{
41
for (int j = 0; j < propertyFileContent.Count; j++)
41
var propertyInfo = profile.PropertiesInfo[i];
42
var property = propertyFileContent.ElementAt(j);
43
if (property.Header == propertyInfo.Name)
42
44
{
43
var propertyInfo = profile.PropertiesInfo[i];
44
var property = propertyFileContent.ElementAt(j);
45
if (property.Header == propertyInfo.Name)
46
{
47
profile.PropertiesInfo[i].Property.SetValue(null, LoadProfilesFunc(property.Content, propertyInfo));
48
continue;
49
}
50
foreach (var propertySecFind in propertyFileContent)
45
profile.PropertiesInfo[i].Property.SetValue(null, LoadProfilesFunc(property.Content, propertyInfo));
46
continue;
47
}
48
foreach (var propertySecFind in propertyFileContent)
49
{
50
if (propertySecFind.Header == propertyInfo.Name)
51
51
{
52
if (propertySecFind.Header == propertyInfo.Name)
53
{
54
profile.PropertiesInfo[i].Property.SetValue(null, LoadProfilesFunc(propertySecFind.Content, propertyInfo));
55
break;
56
}
52
profile.PropertiesInfo[i].Property.SetValue(null, LoadProfilesFunc(propertySecFind.Content, propertyInfo));
53
break;
57
54
}
58
55
}
59
56
}
60
57
}
61
});
58
}
59
}
60
61
/// <summary>
62
/// 加载配置文件
63
/// </summary>
64
/// <param name="profileInfo"></param>
65
/// <returns></returns>
66
public static async Task LoadProfilesAsync(params ProfileInfo[] profileInfo) => await Task.Run(() => LoadProfiles(profileInfo));
67
68
/// <summary>
69
/// 储存指定的配置文件
70
/// </summary>
71
/// <param name="profileInfo">配置文件</param>
72
/// <returns></returns>
73
public static void SaveProfile(ProfileInfo profileInfo)
74
{
75
var waitSaveProfile = Profiles.Find(x => x.Profile == profileInfo.Profile);
76
if (waitSaveProfile is null)
77
return;
78
var saveProfileDictionary = new XFEDictionary();
79
foreach (var property in waitSaveProfile.PropertiesInfo)
80
saveProfileDictionary.Add(property.Name, SaveProfilesFunc(property));
81
var fileSavePath = Path.GetDirectoryName(waitSaveProfile.Path);
82
if (!Directory.Exists(fileSavePath) && fileSavePath is not null && fileSavePath != string.Empty)
83
Directory.CreateDirectory(fileSavePath);
84
File.WriteAllText(waitSaveProfile.Path, saveProfileDictionary);
85
}
86
87
/// <summary>
88
/// 储存配置文件
89
/// </summary>
90
/// <returns></returns>
91
public static void SaveProfiles()
92
{
93
foreach (var profile in Profiles)
94
SaveProfile(profile);
62
95
}
63
96
64
97
/// <summary>
@@ -66,7 +99,7 @@ public abstract class XFEProfile
66
99
/// </summary>
67
100
/// <param name="profileInfo">配置文件</param>
68
101
/// <returns></returns>
69
public static async Task SaveProfile(ProfileInfo profileInfo)
102
public static async Task SaveProfileAsync(ProfileInfo profileInfo)
70
103
{
71
104
var waitSaveProfile = Profiles.Find(x => x.Profile == profileInfo.Profile);
72
105
if (waitSaveProfile is null)
@@ -84,10 +117,10 @@ public abstract class XFEProfile
84
117
/// 储存配置文件
85
118
/// </summary>
86
119
/// <returns></returns>
87
public static async Task SaveProfiles()
120
public static async Task SaveProfilesAsync()
88
121
{
89
122
foreach (var profile in Profiles)
90
await SaveProfile(profile);
123
await SaveProfileAsync(profile);
91
124
}
92
125
93
126
/// <summary>
@@ -107,5 +140,12 @@ public abstract class XFEProfile
107
140
/// </summary>
108
141
/// <param name="profileInfo"></param>
109
142
/// <returns></returns>
110
protected static async Task AutoSave(ProfileInfo profileInfo) => await SaveProfile(profileInfo);
143
protected static void AutoSave(ProfileInfo profileInfo) => SaveProfile(profileInfo);
144
145
/// <summary>
146
/// 可写在属性的set访问器后,用于自动储存
147
/// </summary>
148
/// <param name="profileInfo"></param>
149
/// <returns></returns>
150
protected static async Task AutoSaveAsync(ProfileInfo profileInfo) => await SaveProfileAsync(profileInfo);
111
151
}