C# 继承类的值赋

C# 继承类的值赋

 

       /// <summary>
        /// 将父类的值赋值到子类中
        /// </summary>
        /// <typeparam name="TParent"></typeparam>
        /// <typeparam name="TChild"></typeparam>
        /// <param name="parent"></param>
        /// <param name="child"></param>
        public static void CopyToChild<TParent, TChild>(TParent parent, TChild child)
        {
            var ParentType = typeof(TParent);
            var Properties = ParentType.GetProperties();
            foreach (var Propertie in Properties)
            {
                //循环遍历属性
                if (Propertie.CanRead && Propertie.CanWrite)
                {
                    //进行属性拷贝
                    Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
                }
            }
        }

 

posted on 2019-06-04 17:33  youmeetmehere  阅读(898)  评论(0编辑  收藏  举报

导航