C# 反射模型赋值(List反射模型赋值)

   /// <summary>
        /// 反射模型赋值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="suf"></param>
        /// <param name="strIde"></param>
        /// <returns></returns>
        public static void ObjByPropertyInfoRecurveToT<T>(T model)
        {
            var modelType = model.GetType();
            var enumType = modelType.GetInterface("IEnumerable", false);
            System.Reflection.PropertyInfo[] pros = modelType.GetProperties();
            if (modelType.IsGenericType && enumType != null)
            {
                var listObj = model as IEnumerable<object>;
                foreach (var obj in listObj)
                {
                    ObjByPropertyInfoRecurveToT(obj);
                }
            }
            else 
            {
                foreach (System.Reflection.PropertyInfo pro in pros) 
                {
                    if (pro.PropertyType.IsPrimitive || pro.PropertyType.IsSealed)
                    {
                        var valueObj = pro.GetValue(model, null);
                    }
                    else 
                    {
                        var valueObj = pro.GetValue(model, null);
                        ObjByPropertyInfoRecurveToT(valueObj);
                    }
                        
                }
            }

            

        }

 

posted @ 2021-10-26 16:29  LuoCore  阅读(448)  评论(0编辑  收藏  举报