小工具 --- 树形展示多属性复杂结构类

灵感

最近在做配置模块,然后整个配置的参数是非常多的,层级结构也很深。可能有几百个参数,三、四层的层级关系,想要捋顺所有的类和参数,太繁琐了,而且 Visual Studio 的类视图只能看到属性,却看不出层级关系来,所以花费些许精力,写一个控制台小程序,展示类结构。

原理就是通过反射得到所有属性,遍历展示,有层级关系的递归展示。

代码如下:

public static void PrintProperties(object obj, int indentLevel, ref StringBuilder sb)
{
    try
    {
        var type = obj.GetType();

        var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (var property in properties)
        {
            string[] arr = new string[] { "", "", "" };

            if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
            {
                var value = property.GetValue(obj);

                if (value == null)
                {
                    value = Activator.CreateInstance(property.PropertyType);
                }
                // 判断属性的类型是否为泛型类型
                if (property.PropertyType.IsGenericType)
                {

                    // 获取属性的泛型类型定义
                    var typeDefinition = property.PropertyType.GetGenericTypeDefinition();

                    Type[] typeArgs = value.GetType().GetGenericArguments();

                    var temp = Activator.CreateInstance(typeArgs[0]);

                    // 判断泛型类型是否为 IEnumerable<T>
                    if (typeDefinition == typeof(IEnumerable<>))
                    {
                        arr[0] = "IEnumerable<";
                        arr[1] = typeArgs[0].Name;
                        arr[2] = "> ";
                    }
                    else if (typeDefinition == typeof(List<>))
                    {
                        arr[0] = "List<";
                        arr[1] = typeArgs[0].Name;
                        arr[2] = "> ";
                    }

                    // 判断泛型类型是否为 ICollection<T>
                    else if (typeDefinition == typeof(ICollection<>))
                    {
                        arr[0] = "ICollection<";
                        arr[1] = typeArgs[0].Name;
                        arr[2] = "> ";
                    }
                    else if (typeDefinition == typeof(Dictionary<,>))
                    {
                        arr[0] = "Dictionary<";
                        arr[1] = string.Join(",", typeArgs.Select(t => t.Name));
                        arr[2] = "> ";

                    }

                    sb.Append(new string(' ', indentLevel * 4));
                    sb.AppendLine("- " + string.Join("", arr) + property.Name);

                    PrintProperties(temp, indentLevel + 1, ref sb);
                }
                else
                {
                    arr[0] = "";
                    arr[1] = property.PropertyType.Name;
                    arr[2] = " ";

                    sb.Append(new string(' ', indentLevel * 4));
                    sb.AppendLine("- " + string.Join("", arr) + property.Name);

                    PrintProperties(value, indentLevel + 1, ref sb);
                }
            }
            else
            {
                arr[0] = "";
                arr[1] = property.PropertyType.Name;
                arr[2] = " ";

                sb.Append(new string(' ', indentLevel * 4));
                sb.AppendLine("- " + string.Join("", arr) + property.Name);
            }
        }
    }
    catch (Exception ex)
    {

        throw;
    }
}

示例图:

191b05448444cfd4d1131b2a136edf06.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值