.NET中制做对象的副本(二)继承对象之间的数据拷贝

定义学生

复制代码
/// <summary>
    /// 学生信息
    /// </summary>
    public class Student
    {
        /// <summary>
        /// 学生姓名
        /// </summary>
        public string StudentName { get; set; }
    }
复制代码

定义老师

复制代码
    /// <summary>
    /// 老师信息
    /// </summary>
    public class Teacher
    {
        /// <summary>
        /// 老师姓名
        /// </summary>
        public string TeacherName { get; set; }
        /// <summary>
        /// 学生列表
        /// </summary>
        public List<Student> lists { get; set; }
    }
复制代码

自定教授类

    public class Professor : Teacher
    {

    }

将t1对象拷贝到p1上

复制代码
   Teacher t1 = new Teacher()
            {
                TeacherName = "老师1",
                lists = new List<Student> { new Student { StudentName = "学生1" }, new Student { StudentName = "学生2" } }
            };

            Professor p1 = new Professor()
            {
               
                lists = new List<Student> { },
            };
            CloneEntity(t1, p1);
复制代码

 

实现代码如下

复制代码
        //更新普通属性  
        private static void CloneProperty(PropertyInfo propertyInfo, object intrinsicObj, object newObj)
        {
            var data = propertyInfo.GetValue(newObj, null);
            if (data is IEnumerable<object>)
                CloneNavigationPropertyEntityCollection(propertyInfo, intrinsicObj, newObj);
            else
             propertyInfo.SetValue(intrinsicObj, data, null);
        }


        private static void CloneNavigationPropertyEntityCollection(PropertyInfo propertyInfo, object intrinsicObj, object newObj)
        {
            
            //获取上下文中匹配的原对象集合  
            var intrinsicData = propertyInfo.GetValue(intrinsicObj, null);
            //获取参数newObj中的对象集合  
            IEnumerable<object> newData = propertyInfo.GetValue(newObj, null) as IEnumerable<object>;
            var addMethod = intrinsicData.GetType().GetMethod("Add");
            foreach (object obj in newData)
            {

                Object objClone = GetIntrinsicObj(obj);
                objClone = CloneEntity(obj, objClone);
                addMethod.Invoke(intrinsicData, new object[] { objClone });
            }
        }

        //获取上下文中待更新的原对象  
        private static Object GetIntrinsicObj(Object entity)
        {
            Object intrinsicObj;
            intrinsicObj = Activator.CreateInstance(entity.GetType());

            return intrinsicObj;
        }

        //更新上下文中的原对象,返回值为更新后的原对象  
        public static Object CloneEntity(Object obj, Object intrinsicObj)
        {
            foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
            {
                CloneProperty(propertyInfo, intrinsicObj, obj);
            }
            return intrinsicObj;
        }
复制代码

 下载源代码

posted on   漫思  阅读(506)  评论(0编辑  收藏  举报

编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示