简单的拷贝对象类

 public static class TypeHelper
    {
        public static TP AutoCopy<TP, TC>(TC source) where TP : new()
        {
            TP dest = new TP();

            var ParentType = typeof(TP);

            var Properties = ParentType.GetProperties();

            foreach (var Propertie in Properties)
            {
                if (Propertie.CanRead && Propertie.CanWrite)
                {
                    Propertie.SetValue(dest, Propertie.GetValue(source, null), null);
                }
            }

            return dest;
        }
        public static TP AutoCopy<TP, TC>(TP dest, TC source) where TP : new()
        {
            var ParentType = typeof(TP);

            var Properties = ParentType.GetProperties();

            foreach (var Propertie in Properties)
            {
                if (Propertie.CanRead && Propertie.CanWrite)
                {
                    Propertie.SetValue(dest, Propertie.GetValue(source, null), null);
                }
            }

            return dest;
        }
    }

  

  

posted @ 2014-09-04 15:11  GarySun  阅读(193)  评论(0编辑  收藏  举报