前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小工具 --- 树形展示多属性复杂结构类

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

作者头像
Niuery Diary
发布2023-10-22 16:58:33
1420
发布2023-10-22 16:58:33
举报

灵感

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

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

代码如下:

代码语言:javascript
复制
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;
    }
}

示例图:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-05-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Niuery Diary 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 灵感
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档