首页 > 其他 > 详细

生成树型结构数据,(利用泛型+反射)

时间:2019-11-20 12:39:56      阅读:130      评论:0      收藏:0      [点我收藏+]

直接上代码

1、主要帮助类:TreeDataHelper

    /// <summary>
    /// 树型数据Helper
    /// </summary>
    public class TreeDataHelper
    {
        #region "方法"

        /// <summary>
        /// 返回树型列表(object)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">来源数据</param>
        /// <param name="config">树型配置</param>
        /// <returns></returns>
        public static List<T> ToTreeList<T, S>(List<S> source, TreeDataConfig config) where T : class, new() where S : class, new()
        {
            if (source == null || source.Count <= 0)
            {
                return new List<T>();
            }
            if (config == null)
            {
                config = new TreeDataConfig();
            }
            return toTreeList<T, S>(source, config, config.PIdValue);
        }

        private static List<T> toTreeList<T, S>(List<S> source, TreeDataConfig config, object pidValue) where T : class, new() where S : class, new()
        {
            List<T> returnInfo = new List<T>();
            #region "开始处理"
            foreach (var item in source)
            {
                var itemKeyValue = item.GetType().GetProperty(config.KeyName).GetValue(item);
                var itemPidValue = item.GetType().GetProperty(config.PIdName).GetValue(item);
                if (itemPidValue.Equals(pidValue))
                {
                    T m = new T();
                    //赋值属性
                    foreach (PropertyInfo prop in m.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
                    {
                        var itemPropModel = item.GetType().GetProperty(prop.Name);
                        var itemPropValue = itemPropModel == null ? null : item.GetType().GetProperty(prop.Name).GetValue(item);
                        if (itemPropValue != null)
                        {
                            prop.SetValue(m, itemPropValue);
                        }
                    }
                    //添加子集
                    var childrenProp = m.GetType().GetProperty(config.SonName);
                    childrenProp.SetValue(m, toTreeList<T, S>(source, config, itemKeyValue));
                    //--------
                    returnInfo.Add(m);
                }
            }
            #endregion
            return returnInfo;
        }

        #endregion
    }

 

2、主要配置类:TreeDataConfig

    /// <summary>
    /// 树型配置
    /// </summary>
    public class TreeDataConfig
    {
        #region "构造"

        public TreeDataConfig()
        {
            this.KeyName = "Id";
            this.PIdName = "ParentId";
            this.SonName = "Children";
            this.PIdValue = 0;
        }

        #endregion

        #region "属性"

        /// <summary>
        /// 主键名称
        /// </summary>
        public string KeyName
        {
            get;
            set;
        }

        /// <summary>
        /// 父级名称
        /// </summary>
        public string PIdName
        {
            get;
            set;
        }

        /// <summary>
        /// 子集名称
        /// </summary>
        public string SonName
        {
            get;
            set;
        }

        /// <summary>
        /// 一级父类的值
        /// </summary>
        public object PIdValue
        {
            get;
            set;
        }

        #endregion
    }

 

生成树型结构数据,(利用泛型+反射)

原文:https://www.cnblogs.com/yujian90/p/11896257.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!