反射

1、复制属性

        /// <summary>
        /// 复制属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T1"></typeparam>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        public static void Copy<T, T1>(T source, out T1 destination) where T1 : new()
        {
            destination = new T1();
            Type t = source.GetType();
            Type t1 = destination.GetType();
            PropertyInfo[] PropertyList = t.GetProperties();
            foreach (PropertyInfo item in PropertyList)
            {
                var propertyInfo = t1.GetProperty(item.Name);
                if (propertyInfo != null)
                {
                    propertyInfo.SetValue(destination, item.GetValue(source, null), null); //给对应属性赋值
                }
            }
        }
View Code

 

posted @ 2018-11-19 11:15  江境纣州  阅读(34)  评论(0编辑  收藏  举报