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,现在用户可以自定义储存和加载方法

0e46b4c
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

2 个文件 +28 -4
Modified XFE各类拓展.NetCore.Analyzer/ProfilePropertyAutoGenerator.cs +3 -0
@@ -35,6 +35,9 @@ namespace XFE各类拓展.NetCore.Analyzer
35 35 continue;
36 36 }
37 37 var className = classDeclaration.Identifier.ValueText;
38 var attributeSyntax = SyntaxFactory.AttributeList(
39 SyntaxFactory.SingletonSeparatedList(
40 SyntaxFactory.Attribute(SyntaxFactory.ParseName("global::XFE各类拓展.NetCore.ProfileExtension.ProfileProperty"))));
38 41 var properties = fieldDeclarationSyntaxes.Select(fieldDeclarationSyntax =>
39 42 {
40 43 var variableDeclaration = fieldDeclarationSyntax.Declaration.Variables.First();
Modified XFE各类拓展.NetCore/ProfileExtension/XFEProfile.cs +25 -4
@@ -1,4 +1,5 @@
1 using XFE各类拓展.NetCore.FormatExtension;
1 using System.Reflection;
2 using XFE各类拓展.NetCore.FormatExtension;
2 3
3 4 namespace XFE各类拓展.NetCore.ProfileExtension;
4 5
@@ -7,14 +8,19 @@ namespace XFE各类拓展.NetCore.ProfileExtension;
7 8 /// </summary>
8 9 public abstract class XFEProfile
9 10 {
11 private static Func<ProfileEntryInfo, string> SaveProfilesFunc { get; set; } = x => x.Value is null ? string.Empty : x.Value;
12 private static Func<string, ProfileEntryInfo, object?> LoadProfilesFunc { get; set; } = (x, p) => Convert.ChangeType(x, p.Property.PropertyType);
13
10 14 /// <summary>
11 15 /// 配置文件清单
12 16 /// </summary>
13 17 public static List<ProfileInfo> Profiles { get; private set; } = [];
18
14 19 /// <summary>
15 20 /// 配置文件所在的根目录
16 21 /// </summary>
17 22 public static string ProfilesRootPath { get; set; } = $"{AppDomain.CurrentDomain.BaseDirectory}/Profiles";
23
18 24 /// <summary>
19 25 /// 加载配置文件
20 26 /// </summary>
@@ -38,14 +44,14 @@ public abstract class XFEProfile
38 44 var property = propertyFileContent.ElementAt(j);
39 45 if (property.Header == propertyInfo.Name)
40 46 {
41 profile.PropertiesInfo[i].Property.SetValue(null, Convert.ChangeType(property.Content, propertyInfo.Property.PropertyType));
47 profile.PropertiesInfo[i].Property.SetValue(null, LoadProfilesFunc(property.Content, propertyInfo));
42 48 continue;
43 49 }
44 50 foreach (var propertySecFind in propertyFileContent)
45 51 {
46 52 if (propertySecFind.Header == propertyInfo.Name)
47 53 {
48 profile.PropertiesInfo[i].Property.SetValue(null, Convert.ChangeType(propertySecFind.Content, propertyInfo.Property.PropertyType));
54 profile.PropertiesInfo[i].Property.SetValue(null, LoadProfilesFunc(propertySecFind.Content, propertyInfo));
49 55 break;
50 56 }
51 57 }
@@ -54,6 +60,7 @@ public abstract class XFEProfile
54 60 }
55 61 });
56 62 }
63
57 64 /// <summary>
58 65 /// 储存指定的配置文件
59 66 /// </summary>
@@ -66,12 +73,13 @@ public abstract class XFEProfile
66 73 return;
67 74 var saveProfileDictionary = new XFEDictionary();
68 75 foreach (var property in waitSaveProfile.PropertiesInfo)
69 saveProfileDictionary.Add(property.Name, property.Value is null ? string.Empty : property.Value);
76 saveProfileDictionary.Add(property.Name, SaveProfilesFunc(property));
70 77 var fileSavePath = Path.GetDirectoryName(waitSaveProfile.Path);
71 78 if (!Directory.Exists(fileSavePath) && fileSavePath is not null && fileSavePath != string.Empty)
72 79 Directory.CreateDirectory(fileSavePath);
73 80 await File.WriteAllTextAsync(waitSaveProfile.Path, saveProfileDictionary);
74 81 }
82
75 83 /// <summary>
76 84 /// 储存配置文件
77 85 /// </summary>
@@ -81,6 +89,19 @@ public abstract class XFEProfile
81 89 foreach (var profile in Profiles)
82 90 await SaveProfile(profile);
83 91 }
92
93 /// <summary>
94 /// 设置储存配置文件的方法
95 /// </summary>
96 /// <param name="saveProfilesFunc">储存方法</param>
97 public static void SetSaveProfilesFunction(Func<ProfileEntryInfo, string> saveProfilesFunc) => SaveProfilesFunc = saveProfilesFunc;
98
99 /// <summary>
100 /// 设置加载配置文件的方法
101 /// </summary>
102 /// <param name="loadProfilesFunc">加载方法</param>
103 public static void SetLoadProfilesFunction(Func<string, ProfileEntryInfo, object?> loadProfilesFunc) => LoadProfilesFunc = loadProfilesFunc;
104
84 105 /// <summary>
85 106 /// 可写在属性的set访问器后,用于自动储存
86 107 /// </summary>