XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

添加新的X语法,现在可以分析对象

76125ad
XFE工作室室长 <xfe18827675401@126.com>
提交于

代码差异

13 个文件 +373 -3
Modified XFEExtension.NetCore/StringExtension/StringExtension.cs +8 -0
@@ -1,6 +1,7 @@
1 1 using System.Globalization;
2 2 using System.Text;
3 3 using System.Text.RegularExpressions;
4 using XFEExtension.NetCore.XFETransform;
4 5
5 6 namespace XFEExtension.NetCore.StringExtension;
6 7
@@ -254,6 +255,13 @@ public static partial class StringExtension
254 255 Console.WriteLine(obj);
255 256 }
256 257 /// <summary>
258 /// 分析对象并输出到控制台
259 /// </summary>
260 /// <param name="obj">待分析的对象</param>
261 /// <param name="onlyProperty"></param>
262 /// <param name="onlyPublic"></param>
263 public static void X(this object? obj, bool onlyProperty = true, bool onlyPublic = true) => Console.WriteLine(XFEConverter.GetObjectInfo(obj?.GetType().Name, ObjectPlace.Main, 0, obj?.GetType(), obj, onlyProperty, onlyPublic).OutPut());
264 /// <summary>
257 265 /// 字符串实际显示的长度
258 266 /// </summary>
259 267 /// <param name="str"></param>
Modified XFEExtension.NetCore/XFEChatGPT/MemorableXFEChatGPT.cs +1 -1
@@ -3,7 +3,7 @@ using System.Text;
3 3 using XFEExtension.NetCore.XFEChatGPT.ChatGPTInnerClass.DefaultClass;
4 4 using XFEExtension.NetCore.XFEChatGPT.ChatGPTInnerClass.HelperClass;
5 5 using XFEExtension.NetCore.XFEChatGPT.OtherInnerClass;
6 using XFEExtension.NetCore.XFEJsonTransform;
6 using XFEExtension.NetCore.XFETransform;
7 7
8 8 namespace XFEExtension.NetCore.XFEChatGPT;
9 9
Modified XFEExtension.NetCore/XFEChatGPT/XFEChatGPT.cs +1 -1
@@ -3,7 +3,7 @@ using System.Text;
3 3 using XFEExtension.NetCore.XFEChatGPT.ChatGPTInnerClass.DefaultClass;
4 4 using XFEExtension.NetCore.XFEChatGPT.ChatGPTInnerClass.HelperClass;
5 5 using XFEExtension.NetCore.XFEChatGPT.OtherInnerClass;
6 using XFEExtension.NetCore.XFEJsonTransform;
6 using XFEExtension.NetCore.XFETransform;
7 7
8 8 namespace XFEExtension.NetCore.XFEChatGPT;
9 9
Added XFEExtension.NetCore/XFETransform/IObjectInfo.cs +46 -0
@@ -0,0 +1,46 @@
1 namespace XFEExtension.NetCore.XFETransform;
2
3 /// <summary>
4 /// 对象信息接口
5 /// </summary>
6 public interface IObjectInfo
7 {
8 /// <summary>
9 /// 是否是数组
10 /// </summary>
11 bool IsArray { get; init; }
12 /// <summary>
13 /// 是否是基础类型
14 /// </summary>
15 bool IsBasicType { get; init; }
16 /// <summary>
17 /// 对象名称
18 /// </summary>
19 string? Name { get; init; }
20 /// <summary>
21 /// 子对象
22 /// </summary>
23 ISubObjects? SubObjects { get; init; }
24 /// <summary>
25 /// 对象类型
26 /// </summary>
27 Type Type { get; init; }
28 /// <summary>
29 /// 对象值
30 /// </summary>
31 object? Value { get; init; }
32 /// <summary>
33 /// 对象位置
34 /// </summary>
35 ObjectPlace ObjectPlace { get; init; }
36 /// <summary>
37 /// 对象层级
38 /// </summary>
39 int Layer { get; init; }
40
41 /// <summary>
42 /// 输出对象信息
43 /// </summary>
44 /// <returns></returns>
45 string OutPut();
46 }
Added XFEExtension.NetCore/XFETransform/ISubObjects.cs +13 -0
@@ -0,0 +1,13 @@
1 namespace XFEExtension.NetCore.XFETransform;
2
3 /// <summary>
4 /// 子对象接口
5 /// </summary>
6 public interface ISubObjects : IList<IObjectInfo>
7 {
8 /// <summary>
9 /// 输出子对象信息
10 /// </summary>
11 /// <returns></returns>
12 string OutPutSubObjects();
13 }
Added XFEExtension.NetCore/XFETransform/ObjectInfo.cs +89 -0
@@ -0,0 +1,89 @@
1 namespace XFEExtension.NetCore.XFETransform;
2
3 internal abstract class ObjectInfo : IObjectInfo
4 {
5 public string? Name { get; init; }
6 public ObjectPlace ObjectPlace { get; init; }
7 public int Layer { get; init; }
8 public object? Value { get; init; }
9 public Type Type { get; init; }
10 public bool IsBasicType { get; init; }
11 public bool IsArray { get; init; }
12 public ISubObjects? SubObjects { get; init; }
13
14 public string OutPut()
15 {
16 var outPutString = string.Empty;
17 if (IsBasicType)
18 {
19 if (Value is null)
20 {
21 outPutString += $"{AddObjectPlace()} {XFEConverter.OutPutTypeName(Type)} {Name} : null\n";
22 }
23 else
24 {
25 outPutString += $"{AddObjectPlace()} {XFEConverter.OutPutTypeName(Type)} {Name} : {Value}\n";
26 }
27 }
28 else
29 {
30 var tabString = string.Empty;
31 for (int i = 0; i < Layer; i++)
32 {
33 tabString += "│ ";
34 }
35 if (Layer == 0)
36 {
37 outPutString += $"""
38 {AddObjectPlace()} {XFEConverter.OutPutTypeName(Type)} {Name} :
39 ┌────┬────────────────────────────
40 {SubObjects?.OutPutSubObjects()}
41 └─────────────────────────────────
42
43 """;
44 }
45 else
46 {
47 outPutString += $"""
48 {AddObjectPlace()} {XFEConverter.OutPutTypeName(Type)} {Name} :
49 {tabString}├────┬────────────────────────────
50 {SubObjects?.OutPutSubObjects()}
51 {tabString}└─────────────────────────────────
52
53 """;
54 }
55 }
56 return outPutString;
57 }
58
59 private string AddObjectPlace() => ObjectPlace switch
60 {
61 ObjectPlace.Property => "[属性]",
62 ObjectPlace.Field => "[字段]",
63 ObjectPlace.Main => "[主体]",
64 ObjectPlace.Other => "[无]",
65 _ => string.Empty,
66 };
67
68 public ObjectInfo(string? name, ObjectPlace objectPlace, int layer, Type type, bool isBasicType, object? value = null)
69 {
70 Name = name;
71 ObjectPlace = objectPlace;
72 Layer = layer;
73 Type = type;
74 IsBasicType = isBasicType;
75 IsArray = false;
76 Value = value;
77 }
78 public ObjectInfo(string? name, ObjectPlace objectPlace, int layer, Type type, bool isBasicType, bool isArray, object? value = null, ISubObjects? subObjects = null)
79 {
80 Name = name;
81 ObjectPlace = objectPlace;
82 Layer = layer;
83 Type = type;
84 IsBasicType = isBasicType;
85 IsArray = isArray;
86 Value = value;
87 SubObjects = subObjects;
88 }
89 }
Added XFEExtension.NetCore/XFETransform/ObjectInfoImpl.cs +11 -0
@@ -0,0 +1,11 @@
1 namespace XFEExtension.NetCore.XFETransform;
2
3 internal class ObjectInfoImpl : ObjectInfo
4 {
5 public ObjectInfoImpl(string? name, ObjectPlace objectPlace, int layer, Type type, bool isBasicType, object? value = null) : base(name, objectPlace, layer, type, isBasicType, value)
6 {
7 }
8 public ObjectInfoImpl(string? name, ObjectPlace objectPlace, int layer, Type type, bool isBasicType, bool isArray, object? value, ISubObjects? subObjects = null) : base(name, objectPlace, layer, type, isBasicType, isArray, value, subObjects)
9 {
10 }
11 }
Added XFEExtension.NetCore/XFETransform/ObjectPlace.cs +24 -0
@@ -0,0 +1,24 @@
1 namespace XFEExtension.NetCore.XFETransform;
2
3 /// <summary>
4 /// 对象位置
5 /// </summary>
6 public enum ObjectPlace
7 {
8 /// <summary>
9 /// 属性
10 /// </summary>
11 Property,
12 /// <summary>
13 /// 字段
14 /// </summary>
15 Field,
16 /// <summary>
17 /// 主对象
18 /// </summary>
19 Main,
20 /// <summary>
21 /// 其他
22 /// </summary>
23 Other
24 }
Added XFEExtension.NetCore/XFETransform/SubObjects.cs +31 -0
@@ -0,0 +1,31 @@
1 namespace XFEExtension.NetCore.XFETransform;
2
3 internal abstract class SubObjects : SubObjectsBase
4 {
5 public override string OutPutSubObjects()
6 {
7 var outString = string.Empty;
8 for (int i = 0; i < objectInfoList.Count; i++)
9 {
10 var obj = objectInfoList[i];
11 for (int j = 0; j < obj.Layer; j++)
12 {
13 outString += "│ ";
14 }
15 if (i == objectInfoList.Count - 1)
16 {
17 outString += "└─";
18 }
19 else
20 {
21 outString += "├─";
22 }
23 outString += obj.OutPut();
24 }
25 if (outString[^1] == '\n')
26 {
27 outString = outString[..^1];
28 }
29 return outString;
30 }
31 }
Added XFEExtension.NetCore/XFETransform/SubObjectsBase.cs +36 -0
@@ -0,0 +1,36 @@
1 using System.Collections;
2
3 namespace XFEExtension.NetCore.XFETransform;
4
5 internal abstract class SubObjectsBase : ISubObjects
6 {
7 protected readonly List<IObjectInfo> objectInfoList = [];
8
9 public IObjectInfo this[int index] { get => objectInfoList[index]; set => objectInfoList[index] = value;}
10
11 public int Count => objectInfoList.Count;
12
13 public bool IsReadOnly => false;
14
15 public void Add(IObjectInfo item) => objectInfoList.Add(item);
16
17 public void Clear() => objectInfoList.Clear();
18
19 public bool Contains(IObjectInfo item) => objectInfoList.Contains(item);
20
21 public void CopyTo(IObjectInfo[] array, int arrayIndex) => objectInfoList.CopyTo(array, arrayIndex);
22
23 public IEnumerator<IObjectInfo> GetEnumerator() => objectInfoList.GetEnumerator();
24
25 public int IndexOf(IObjectInfo item) => objectInfoList.IndexOf(item);
26
27 public void Insert(int index, IObjectInfo item) => objectInfoList.Insert(index, item);
28
29 public abstract string OutPutSubObjects();
30
31 public bool Remove(IObjectInfo item) => objectInfoList.Remove(item);
32
33 public void RemoveAt(int index) => objectInfoList.RemoveAt(index);
34
35 IEnumerator IEnumerable.GetEnumerator() => objectInfoList.GetEnumerator();
36 }
Added XFEExtension.NetCore/XFETransform/SubObjectsImpl.cs +6 -0
@@ -0,0 +1,6 @@
1 namespace XFEExtension.NetCore.XFETransform
2 {
3 internal class SubObjectsImpl : SubObjects
4 {
5 }
6 }
Added XFEExtension.NetCore/XFETransform/XFEConverter.cs +106 -0
@@ -0,0 +1,106 @@
1 using System.Collections;
2 using System.Reflection;
3
4 namespace XFEExtension.NetCore.XFETransform;
5
6 /// <summary>
7 /// XFE转换器
8 /// </summary>
9 public class XFEConverter
10 {
11 /// <summary>
12 /// 判断是否为基础类型
13 /// </summary>
14 /// <param name="type">类型</param>
15 /// <returns></returns>
16 public static bool IsBasicType(Type type)
17 {
18 return type == typeof(string) || type == typeof(int) || type == typeof(double) || type == typeof(float) || type == typeof(decimal) || type == typeof(long) || type == typeof(short) || type == typeof(byte) || type == typeof(bool) || type == typeof(char) || type == typeof(DateTime);
19 }
20
21 /// <summary>
22 /// 将基础类的文本转换为代码类型
23 /// </summary>
24 /// <param name="originString">原文本</param>
25 /// <returns></returns>
26 public static string ConvertBasicTypeToCodeType(string originString) => originString.ToLower().Replace("int32", "int").Replace("int64", "long").Replace("int16", "short").Replace("boolean", "bool").Replace("datetime", "DateTime");
27
28 /// <summary>
29 /// 输出类型名称
30 /// </summary>
31 /// <param name="type">类型</param>
32 /// <returns></returns>
33 public static string OutPutTypeName(Type type)
34 {
35 if (IsBasicType(type))
36 return ConvertBasicTypeToCodeType(type.Name);
37 var genericArguments = type.GetGenericArguments();
38 if (genericArguments.Length == 0)
39 return type.Name;
40 return $"{type.Name}<{string.Join(", ", genericArguments.Select(x =>
41 {
42 if (IsBasicType(x))
43 return ConvertBasicTypeToCodeType(x.Name);
44 return x.Name;
45 }))}>";
46 }
47
48 /// <summary>
49 /// 获取对象信息
50 /// </summary>
51 /// <param name="name">对象名称</param>
52 /// <param name="objectPlace">对象位置</param>
53 /// <param name="layer">嵌套层数</param>
54 /// <param name="type">类型</param>
55 /// <param name="value">值</param>
56 /// <param name="onlyProperty">是否只获取属性</param>
57 /// <param name="onlyPublic">是否只获取公共成员</param>
58 /// <returns></returns>
59 public static IObjectInfo GetObjectInfo(string? name, ObjectPlace objectPlace, int layer, Type? type, object? value = null, bool onlyProperty = true, bool onlyPublic = true)
60 {
61 if (value is null || type is null)
62 {
63 return new ObjectInfoImpl(name, objectPlace, layer, typeof(Nullable), true);
64 }
65 var isBasicType = IsBasicType(type);
66 if (isBasicType)
67 return new ObjectInfoImpl(name, objectPlace, layer, type, true, value);
68 else if (value is null)
69 return new ObjectInfoImpl(name, objectPlace, layer, type, false, false);
70 else
71 {
72 if (type.IsAssignableTo(typeof(IEnumerable)))
73 {
74 var enumerableObjects = new SubObjectsImpl();
75 foreach (var item in (IEnumerable)value)
76 {
77 enumerableObjects.Add(GetObjectInfo("", ObjectPlace.Other, layer + 1, item.GetType(), item, onlyProperty, onlyPublic));
78 }
79 return new ObjectInfoImpl(name, ObjectPlace.Other, layer, type, false, true, value, enumerableObjects);
80 }
81 if (type.IsAssignableTo(typeof(Array)))
82 {
83 var arrayObjects = new SubObjectsImpl();
84 foreach (var item in (Array)value)
85 {
86 arrayObjects.Add(GetObjectInfo("", ObjectPlace.Other, layer + 1, item.GetType(), item, onlyProperty, onlyPublic));
87 }
88 return new ObjectInfoImpl(name, ObjectPlace.Other, layer, type, false, true, value, arrayObjects);
89 }
90 var subObjects = new SubObjectsImpl();
91 foreach (var memberInfo in type.GetMembers((onlyPublic ? BindingFlags.Public : BindingFlags.NonPublic | BindingFlags.Public) | BindingFlags.Instance | BindingFlags.Static))
92 {
93 if (memberInfo is PropertyInfo propertyInfo)
94 {
95 subObjects.Add(GetObjectInfo(propertyInfo.Name, ObjectPlace.Property, layer + 1, propertyInfo.PropertyType, propertyInfo.GetValue(value), onlyProperty, onlyPublic));
96 continue;
97 }
98 if (!onlyProperty && memberInfo is FieldInfo field)
99 {
100 subObjects.Add(GetObjectInfo(field.Name, ObjectPlace.Field, layer + 1, field.FieldType, field.GetValue(value), onlyProperty, onlyPublic));
101 }
102 }
103 return new ObjectInfoImpl(name, objectPlace, layer, type, false, false, value, subObjects);
104 }
105 }
106 }
Renamed XFEExtension.NetCore/XFETransform/XFEJsonTransformer.cs +1 -1