首页 > Windows开发 > 详细

C# 同类型实体赋值

时间:2016-12-16 22:41:07      阅读:303      评论:0      收藏:0      [点我收藏+]
        #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# 同类型实体赋值

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

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