【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,
}

posted on   ciciJun  阅读(26)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示