【C#】获取实体类属性基本信息
暂时先将就用一下
// See https://aka.ms/new-console-template for more information
using Newtonsoft.Json;
var type = typeof(A);
var i = Get(type);
var jsonSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
var result = JsonConvert.SerializeObject(i, Formatting.Indented, jsonSetting);
Console.WriteLine(result);
Console.ReadKey();
static List<PropertyInfo> Get(Type t)
{
var propList = new List<PropertyInfo>();
var props = t.GetProperties();
foreach (var prop in props)
{
var p = new PropertyInfo();
p.Name = prop.Name;
p.CanBeNull = false;
p.TypeName = prop.PropertyType.Name;
if (prop.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) != null && prop.PropertyType != typeof(string))
{
p.CanBeNull = true;
if (prop.PropertyType.BaseType == typeof(Array))
{
p.CanBeNull = false;
p.TypeName = "Arrary<" + prop.PropertyType.GetElementType().Name + ">";
}
else
{
p.TypeName = "Arrary<" + prop.PropertyType.GenericTypeArguments[0].Name + ">";
p.ChildProperties = Get(prop.PropertyType.GenericTypeArguments[0]);
}
}
else if (prop.PropertyType.Name == typeof(Nullable<>).Name)
{
p.TypeName = prop.PropertyType.GenericTypeArguments[0].Name;
p.CanBeNull = true;
if (prop.PropertyType.GenericTypeArguments[0].IsEnum)
{
p.EnumValues = GetEnumString(prop.PropertyType.GenericTypeArguments[0]);
}
}
else if (prop.PropertyType.IsEnum)
{
p.EnumValues = GetEnumString(prop.PropertyType);
}
else if (prop.PropertyType == typeof(string))
{
p.CanBeNull = true;
}
else if (!prop.PropertyType.IsPrimitive && !prop.PropertyType.IsEnum)
{
p.CanBeNull = true;
p.ChildProperties = Get(prop.PropertyType);
}
if (p.ChildProperties != null && p.ChildProperties.Count == 0)
{
p.ChildProperties = null;
}
propList.Add(p);
}
return propList;
}
static string[] GetEnumString(Type type)
{
List<string> enumValues = new List<string>();
if (type.IsEnum)
{
var values = type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
for (int i = 0; i < values.Length; i++)
{
enumValues.Add(values[i].ToString() + "=" + (int)values[i].GetValue(null));
}
}
return enumValues.ToArray();
}
public class PropertyInfo
{
public string Name { get; set; }
public string TypeName { get; set; }
public List<PropertyInfo> ChildProperties { get; set; }
public bool CanBeNull { get; set; } = true;
public string[] EnumValues { get; set; }
}
public class A
{
public int[] Arr { get; set; }
public List<C> C { get; set; }
public B B { get; set; }
public int? Id2 { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public bool IsOk { get; set; }
}
public class B
{
public Guid Id { get; set; }
public int Number { get; set; }
}
public class C
{
public MyEnum? MyEnum { get; set; }
}
public enum MyEnum
{
enum1 = 1,
enum2 = 2,
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下