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

7aeb7c1
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

7 个文件 +161 -74
Modified XFEExtension.NetCore.Analyzer.Test/Program.cs +24 -4
@@ -1,12 +1,24 @@
1 namespace XFEExtension.NetCore.Analyzer.Test;
1 using XFEExtension.NetCore.StringExtension;
2 using XFEExtension.NetCore.XFETransform.ObjectInfoAnalyzer;
3 using XFEExtension.NetCore.XFETransform;
4 using XFEExtension.NetCore.XFETransform.StringConverter;
5
6 namespace XFEExtension.NetCore.Analyzer.Test;
2 7
3 8 internal class Program
4 9 {
5 10 [SMTest]
6 public static void TestNamedPipeAsync()
11 public static async void TestNamedPipeAsync()
7 12 {
8 Console.WriteLine("Hello World!");
9 Console.WriteLine("Program End");
13 var testClass = new TestClass("测试类", "测试描述:我上早八", 56)
14 {
15 Tags = [["标签11", "标签12"], ["标签21", "标签22"]],
16 Enum = MyEnum.Test2,
17 MultiEnum = MyMultiEnum.Get | MyMultiEnum.Pull | MyMultiEnum.Set | MyMultiEnum.Post
18 }.GetType();
19 var result = XFEConverter.GetObjectInfo(StringConverter.ColoredObjectAnalyzer, "分析对象", ObjectPlace.Main, 0, [testClass], testClass?.GetType(), testClass, false, false).OutPutObject();
20 Console.WriteLine(result);
21 //testClass.X();
10 22 }
11 23 }
12 24 enum MyEnum
@@ -14,4 +26,12 @@ enum MyEnum
14 26 Test1 = 3,
15 27 Test2 = 12,
16 28 Test3 = 1
29 }
30 [Flags]
31 enum MyMultiEnum
32 {
33 Get = 1,
34 Set = 2,
35 Post = 4,
36 Pull = 8
17 37 }
Modified XFEExtension.NetCore.Analyzer.Test/TestClass.cs +1 -0
@@ -10,6 +10,7 @@ internal class TestClass(string name, string description, int age)
10 10 public List<List<string>> Tags { get; set; }
11 11 public bool MyProperty { get; set; }
12 12 public MyEnum Enum { get; set; }
13 public MyMultiEnum MultiEnum { get; set; }
13 14 public string Description { get; set; } = description;
14 15 public int Age { get; set; } = age;
15 16 }
Modified XFEExtension.NetCore/StringExtension/JsonStringExtension.cs +1 -1
@@ -52,5 +52,5 @@ public static class JsonStringExtension
52 52 /// </summary>
53 53 /// <param name="obj"></param>
54 54 /// <returns></returns>
55 public static string ToJson(this object obj) => XFEConverter.GetObjectInfo(StringConverter.JsonTransformer, string.Empty, ObjectPlace.Main, 0, obj?.GetType(), obj).OutPutObject();
55 public static string ToJson(this object obj) => XFEConverter.GetObjectInfo(StringConverter.JsonTransformer, string.Empty, ObjectPlace.Main, 0, [obj], obj?.GetType(), obj).OutPutObject();
56 56 }
Modified XFEExtension.NetCore/StringExtension/StringExtension.cs +2 -2
@@ -266,7 +266,7 @@ public static partial class StringExtension
266 266 /// <param name="onlyPublic"></param>
267 267 public static string? X(this object? obj, bool onlyProperty = true, bool onlyPublic = true, string remarkName = "分析对象")
268 268 {
269 var result = XFEConverter.GetObjectInfo(StringConverter.ObjectAnalyzer, remarkName, ObjectPlace.Main, 0, obj?.GetType(), obj, onlyProperty, onlyPublic).OutPutObject();
269 var result = XFEConverter.GetObjectInfo(StringConverter.ObjectAnalyzer, remarkName, ObjectPlace.Main, 0, [obj], obj?.GetType(), obj, onlyProperty, onlyPublic).OutPutObject();
270 270 Console.WriteLine(result);
271 271 return result;
272 272 }
@@ -280,7 +280,7 @@ public static partial class StringExtension
280 280 /// <param name="onlyPublic"></param>
281 281 public static string XL(this object? obj, string remarkName = "分析对象", bool onlyProperty = true, bool onlyPublic = true)
282 282 {
283 var result = XFEConverter.GetObjectInfo(StringConverter.ObjectAnalyzer, remarkName, ObjectPlace.Main, 0, obj?.GetType(), obj, onlyProperty, onlyPublic).OutPutObject();
283 var result = XFEConverter.GetObjectInfo(StringConverter.ObjectAnalyzer, remarkName, ObjectPlace.Main, 0, [obj], obj?.GetType(), obj, onlyProperty, onlyPublic).OutPutObject();
284 284 Trace.WriteLine(result);
285 285 return result;
286 286 }
Modified XFEExtension.NetCore/XFETransform/StringConverter/ColoredObjectAnalyzer.cs +13 -8
@@ -23,17 +23,19 @@ public class ColoredObjectAnalyzer : StringConverter
23 23 }
24 24 else if (objectInfo.ObjectPlace == ObjectPlace.Enum)
25 25 {
26 outPutString += $"{AddObjectPlace(objectInfo)} [color #b5cea8]{XFEConverter.OutPutTypeName(objectInfo.Type!)} [color white]{objectInfo.Name}:[color #d69d85]{objectInfo.Value}[color #569cd6][[color #b5cea8]{(int)objectInfo.Value}[color #569cd6]][color white]\n";
26 string enumValue = "[color red]无法获取[color white]";
27 try { enumValue = ((int)objectInfo.Value).ToString(); } catch { }
28 outPutString += $"{AddObjectPlace(objectInfo)} [color #b5cea8]{XFEConverter.OutPutTypeName(objectInfo.Type!)} [color white]{objectInfo.Name}:[color #d69d85]{objectInfo.Value}[color #569cd6][[color #b5cea8]{enumValue}[color #569cd6]][color white]\n";
27 29 }
28 30 else if (objectInfo.Value is string str && str == string.Empty)
29 31 {
30 32 outPutString += $"{AddObjectPlace(objectInfo)} [color #569cd6]{XFEConverter.OutPutTypeName(objectInfo.Type!)}[color white] {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}[color white]:[color #569cd6]string[color white].Empty\n";
31 33 }
32 else if(objectInfo.Value is string)
34 else if (objectInfo.Value is string)
33 35 {
34 36 outPutString += $"{AddObjectPlace(objectInfo)} [color #569cd6]{XFEConverter.OutPutTypeName(objectInfo.Type!)}[color white] {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}[color white]:[color #d69d85]{objectInfo.Value}[color white]\n";
35 37 }
36 else if(objectInfo.Value is float || objectInfo.Value is int || objectInfo.Value is double || objectInfo.Value is long || objectInfo.Value is short || objectInfo.Value is byte || objectInfo.Value is uint || objectInfo.Value is ulong || objectInfo.Value is ushort || objectInfo.Value is decimal)
38 else if (objectInfo.Value is float || objectInfo.Value is int || objectInfo.Value is double || objectInfo.Value is long || objectInfo.Value is short || objectInfo.Value is byte || objectInfo.Value is uint || objectInfo.Value is ulong || objectInfo.Value is ushort || objectInfo.Value is decimal)
37 39 {
38 40 outPutString += $"{AddObjectPlace(objectInfo)} [color #569cd6]{XFEConverter.OutPutTypeName(objectInfo.Type!)}[color white] {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}[color white]:[color #b5cea8]{objectInfo.Value}[color white]\n";
39 41 }
@@ -46,14 +48,17 @@ public class ColoredObjectAnalyzer : StringConverter
46 48 {
47 49 if (objectInfo.Layer == 0)
48 50 {
49 if(objectInfo.SubObjects == null)
51 if (objectInfo.SubObjects == null)
50 52 {
51 outPutString += $"{AddObjectPlace(objectInfo)} [color #4ec9b0]{XFEConverter.OutPutTypeName(objectInfo.Type!)} {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}: [color #569cd6]null[color white]";
53 if (objectInfo.Value is null)
54 outPutString += $"{AddObjectPlace(objectInfo)} [color #4ec9b0]{XFEConverter.OutPutTypeName(objectInfo.Type!)} {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}[color white]: [color #569cd6]null[color white]";
55 else
56 outPutString += $"{AddObjectPlace(objectInfo)} [color #4ec9b0]{XFEConverter.OutPutTypeName(objectInfo.Type!)} {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}[color white]: {objectInfo.Value}[color white]";
52 57 }
53 58 else if (objectInfo.SubObjects?.Count > 0)
54 59 {
55 60 outPutString += $"""
56 {AddObjectPlace(objectInfo)} [color #4ec9b0]{XFEConverter.OutPutTypeName(objectInfo.Type!)} {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}
61 {AddObjectPlace(objectInfo)} [color #4ec9b0]{XFEConverter.OutPutTypeName(objectInfo.Type!)} {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}[color white]
57 62 ┌────┬────────────────────────────
58 63 {OutPutSubObjects(objectInfo.SubObjects)}
59 64 └─────────────────────────────────
@@ -62,7 +67,7 @@ public class ColoredObjectAnalyzer : StringConverter
62 67 else
63 68 {
64 69 outPutString += $"""
65 {AddObjectPlace(objectInfo)} [color #4ec9b0]{XFEConverter.OutPutTypeName(objectInfo.Type!)} {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}
70 {AddObjectPlace(objectInfo)} [color #4ec9b0]{XFEConverter.OutPutTypeName(objectInfo.Type!)} {AddObjectPlaceColor(objectInfo)}{objectInfo.Name}[color white]
66 71 ┌────┬────────────────────────────
67 72 │ └─[color #569cd6][无内容][color white]"
68 73 └─────────────────────────────────
@@ -129,7 +134,7 @@ public class ColoredObjectAnalyzer : StringConverter
129 134 else
130 135 {
131 136 outString += $"""
132 ├─{AddObjectPlace(obj)} [color #4ec9b0]{XFEConverter.OutPutTypeName(obj.Type!)} {AddObjectPlaceColor(obj)}{obj.Name}
137 ├─{AddObjectPlace(obj)} [color #4ec9b0]{XFEConverter.OutPutTypeName(obj.Type!)} {AddObjectPlaceColor(obj)}{obj.Name}[color white]
133 138 {tabString}├────┬────────────────────────────
134 139 {obj.OutPutObject()}
135 140 {tabString}{currentConnectString}────────────────────────────────
Modified XFEExtension.NetCore/XFETransform/StringConverter/ObjectAnalyzer.cs +5 -2
@@ -38,9 +38,12 @@ public class ObjectAnalyzer : StringConverter
38 38 {
39 39 if (objectInfo.Layer == 0)
40 40 {
41 if(objectInfo.SubObjects == null)
41 if (objectInfo.SubObjects == null)
42 42 {
43 outPutString += $"{AddObjectPlace(objectInfo)} {XFEConverter.OutPutTypeName(objectInfo.Type!)} {objectInfo.Name}: null";
43 if (objectInfo.Value is null)
44 outPutString += $"{AddObjectPlace(objectInfo)} {XFEConverter.OutPutTypeName(objectInfo.Type!)} {objectInfo.Name}: null";
45 else
46 outPutString += $"{AddObjectPlace(objectInfo)} {XFEConverter.OutPutTypeName(objectInfo.Type!)} {objectInfo.Name}: {objectInfo.Value}";
44 47 }
45 48 else if (objectInfo.SubObjects?.Count > 0)
46 49 {
Modified XFEExtension.NetCore/XFETransform/XFEConverter.cs +115 -57
@@ -47,6 +47,20 @@ public class XFEConverter
47 47 }))}>";
48 48 }
49 49
50 /// <summary>
51 /// 添加并赋值父类列表
52 /// </summary>
53 /// <param name="originalFatherList"></param>
54 /// <param name="value"></param>
55 /// <returns></returns>
56 public static List<object> AddAndCopyFatherList(List<object> originalFatherList, object value)
57 {
58 var newList = new List<object>();
59 newList.AddRange(originalFatherList);
60 newList.Add(value);
61 return newList;
62 }
63
50 64 /// <summary>
51 65 /// 获取对象信息
52 66 /// </summary>
@@ -54,90 +68,134 @@ public class XFEConverter
54 68 /// <param name="name">对象名称</param>
55 69 /// <param name="objectPlace">对象位置</param>
56 70 /// <param name="layer">嵌套层数</param>
71 /// <param name="fatherObjectList">父类列表</param>
57 72 /// <param name="type">类型</param>
58 73 /// <param name="value">值</param>
59 74 /// <param name="onlyProperty">是否只获取属性</param>
60 75 /// <param name="onlyPublic">是否只获取公共成员</param>
76 /// <param name="stopInThisLayer">是否在该层停止递归</param>
77 /// <param name="maxLayer">最大解析深度</param>
78 /// <param name="maxArrayCount">数组列表的最大解析数量</param>
61 79 /// <returns></returns>
62 public static IObjectInfo GetObjectInfo(IStringConverter? stringConverter, string? name, ObjectPlace objectPlace, int layer, Type? type, object? value = null, bool onlyProperty = true, bool onlyPublic = true)
80 public static IObjectInfo GetObjectInfo(IStringConverter? stringConverter, string? name, ObjectPlace objectPlace, int layer, List<object> fatherObjectList, Type? type, object? value = null, bool onlyProperty = true, bool onlyPublic = true, bool stopInThisLayer = false, int maxLayer = 10, int maxArrayCount = 999)
63 81 {
64 if (value is null || type is null)
65 {
66 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, true);
67 }
68 var isBasicType = IsBasicType(type);
69 if (isBasicType)
70 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, true, value);
71 else
82 try
72 83 {
73 if (type.IsAssignableTo(typeof(Array)))
84 if (value is null || type is null)
74 85 {
75 var arrayObjects = new List<IObjectInfo>();
76 foreach (var item in (Array)value)
77 {
78 try
79 {
80 arrayObjects.Add(GetObjectInfo(stringConverter, "数组成员", ObjectPlace.ArrayMember, layer + 1, item.GetType(), item, onlyProperty, onlyPublic));
81 }
82 catch
83 {
84 arrayObjects.Add(GetObjectInfo(stringConverter, "数组成员", ObjectPlace.ArrayMember, layer + 1, null, null, onlyProperty, onlyPublic));
85 }
86 }
87 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, true, value, arrayObjects);
86 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, true);
88 87 }
89 else if (type.IsAssignableTo(typeof(IEnumerable)))
88 if (fatherObjectList.Contains(value) && layer > 0)
90 89 {
91 var enumerableObjects = new List<IObjectInfo>();
92 foreach (var item in (IEnumerable)value)
93 {
94 try
95 {
96 enumerableObjects.Add(GetObjectInfo(stringConverter, "列表成员", ObjectPlace.ListMember, layer + 1, item.GetType(), item, onlyProperty, onlyPublic));
97 }
98 catch
99 {
100 enumerableObjects.Add(GetObjectInfo(stringConverter, "列表成员", ObjectPlace.ListMember, layer + 1, null, null, onlyProperty, onlyPublic));
101 }
102 }
103 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, true, value, enumerableObjects);
90 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, true, "重复递归异常");
104 91 }
105 else if (type.IsAssignableTo(typeof(Enum)))
92 if (layer > maxLayer)
106 93 {
107 return new ObjectInfoImpl(stringConverter, name, ObjectPlace.Enum, layer, type, false, value);
94 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, true, $"超过最大解析深度({maxLayer})");
108 95 }
109 else if (type.IsValueType)
96 var currentFatherList = AddAndCopyFatherList(fatherObjectList, value);
97 var isBasicType = IsBasicType(type);
98 if (isBasicType)
99 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, true, value);
100 else
110 101 {
111 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, value);
112 }
113 var subObjects = new List<IObjectInfo>();
114 foreach (var memberInfo in type.GetMembers((onlyPublic ? BindingFlags.Public : BindingFlags.NonPublic | BindingFlags.Public) | BindingFlags.Instance | BindingFlags.Static))
115 {
116 if (memberInfo is PropertyInfo propertyInfo)
102
103 if (type.IsAssignableTo(typeof(Enum)))
104 {
105 return new ObjectInfoImpl(stringConverter, name, ObjectPlace.Enum, layer, type, false, value);
106 }
107 else if (type.IsValueType)
108 {
109 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, value);
110 }
111 else if (value is Exception exception)
117 112 {
118 try
113 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, exception.ToString());
114 }
115 else if (type.IsAssignableTo(typeof(Array)))
116 {
117 var arrayObjects = new List<IObjectInfo>();
118 var currentCount = 0;
119 foreach (var item in (Array)value)
119 120 {
120 subObjects.Add(GetObjectInfo(stringConverter, propertyInfo.Name, ObjectPlace.Property, layer + 1, propertyInfo.PropertyType, propertyInfo.GetValue(value), onlyProperty, onlyPublic));
121 currentCount++;
122 try
123 {
124 arrayObjects.Add(GetObjectInfo(stringConverter, "数组成员", ObjectPlace.ArrayMember, layer + 1, currentFatherList, item.GetType(), item, onlyProperty, onlyPublic, stopInThisLayer, maxLayer, maxArrayCount));
125 }
126 catch
127 {
128 arrayObjects.Add(GetObjectInfo(stringConverter, "数组成员", ObjectPlace.ArrayMember, layer + 1, currentFatherList, null, null, onlyProperty, onlyPublic, stopInThisLayer, maxLayer, maxArrayCount));
129 }
130 if (currentCount > maxArrayCount)
131 {
132 arrayObjects.Add(new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, true, $"数组数量超出设定最大值({maxArrayCount})"));
133 break;
134 }
121 135 }
122 catch
136 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, true, value, arrayObjects);
137 }
138 else if (type.IsAssignableTo(typeof(IEnumerable)))
139 {
140 var enumerableObjects = new List<IObjectInfo>();
141 var currentCount = 0;
142 foreach (var item in (IEnumerable)value)
123 143 {
124 subObjects.Add(GetObjectInfo(stringConverter, propertyInfo.Name, ObjectPlace.Property, layer + 1, propertyInfo.PropertyType, null, onlyProperty, onlyPublic));
144 currentCount++;
145 try
146 {
147 enumerableObjects.Add(GetObjectInfo(stringConverter, "列表成员", ObjectPlace.ListMember, layer + 1, currentFatherList, item.GetType(), item, onlyProperty, onlyPublic, stopInThisLayer, maxLayer, maxArrayCount));
148 }
149 catch
150 {
151 enumerableObjects.Add(GetObjectInfo(stringConverter, "列表成员", ObjectPlace.ListMember, layer + 1, currentFatherList, null, null, onlyProperty, onlyPublic, stopInThisLayer, maxLayer, maxArrayCount));
152 }
153 if (currentCount > maxArrayCount)
154 {
155 enumerableObjects.Add(new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, true, $"列表数量超出设定最大值({maxArrayCount})"));
156 break;
157 }
125 158 }
126 continue;
159 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, true, value, enumerableObjects);
127 160 }
128 if (!onlyProperty && memberInfo is FieldInfo fieldInfo)
161 if (stopInThisLayer)
129 162 {
130 try
163 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, false, value);
164 }
165 var isType = value is Type || type.FullName == "System.RuntimeType";
166 var subObjects = new List<IObjectInfo>();
167 foreach (var memberInfo in type.GetMembers((onlyPublic ? BindingFlags.Public : BindingFlags.NonPublic | BindingFlags.Public) | BindingFlags.Instance | BindingFlags.Static))
168 {
169 if (memberInfo is PropertyInfo propertyInfo)
131 170 {
132 subObjects.Add(GetObjectInfo(stringConverter, fieldInfo.Name, ObjectPlace.Field, layer + 1, fieldInfo.FieldType, fieldInfo.GetValue(value), onlyProperty, onlyPublic));
171 try
172 {
173 subObjects.Add(GetObjectInfo(stringConverter, propertyInfo.Name, ObjectPlace.Property, layer + 1, currentFatherList, propertyInfo.PropertyType, propertyInfo.GetValue(value), onlyProperty, onlyPublic, isType, maxLayer, maxArrayCount));
174 }
175 catch
176 {
177 subObjects.Add(GetObjectInfo(stringConverter, propertyInfo.Name, ObjectPlace.Property, layer + 1, currentFatherList, propertyInfo.PropertyType, null, onlyProperty, onlyPublic, isType, maxLayer, maxArrayCount));
178 }
179 continue;
133 180 }
134 catch
181 if (!onlyProperty && memberInfo is FieldInfo fieldInfo)
135 182 {
136 subObjects.Add(GetObjectInfo(stringConverter, fieldInfo.Name, ObjectPlace.Property, layer + 1, fieldInfo.FieldType, null, onlyProperty, onlyPublic));
183 try
184 {
185 subObjects.Add(GetObjectInfo(stringConverter, fieldInfo.Name, ObjectPlace.Field, layer + 1, currentFatherList, fieldInfo.FieldType, fieldInfo.GetValue(value), onlyProperty, onlyPublic, isType, maxLayer, maxArrayCount));
186 }
187 catch
188 {
189 subObjects.Add(GetObjectInfo(stringConverter, fieldInfo.Name, ObjectPlace.Property, layer + 1, currentFatherList, fieldInfo.FieldType, null, onlyProperty, onlyPublic, isType, maxLayer, maxArrayCount));
190 }
137 191 }
138 192 }
193 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, false, value, subObjects);
139 194 }
140 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, false, false, value, subObjects);
195 }
196 catch (Exception ex)
197 {
198 return new ObjectInfoImpl(stringConverter, name, objectPlace, layer, type, true, $"无法获取:{ex.Message} ");
141 199 }
142 200 }
143 201 }