码迷,mamicode.com
首页 > Windows程序 > 详细

C# 同类型实体赋值

时间:2016-12-16 22:41:31      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:for   turn   c#   activator   cti   default   shadow   sha   void   

        #region 更新赋值,前者赋值给后者
        public static void ShadowCopy(object a, object b)
        {
            if (a == null) return;

            if (a.GetType() == b.GetType())
            {
                PropertyInfo[] properties = a.GetType().GetProperties();

                foreach (var p in properties)
                {
                    var value = p.GetValue(a, null);
                    if (value != GetDefault(p.GetType()))
                        p.SetValue(b, value, null);
                }
            }
        }

        public static object GetDefault(Type type)
        {
            if (type.IsValueType)
            {
                return Activator.CreateInstance(type);
            }
            return null;
        }

        #endregion

 

C# 同类型实体赋值

标签:for   turn   c#   activator   cti   default   shadow   sha   void   

原文地址:http://www.cnblogs.com/justqi/p/6188542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!