.net 浅复制

你可以使用C#编程语言来编写一个通用的扩展方法,用于将一个对象的值复制到另一个对象,并且修改目标对象的属性时原始对象不受影响。

以下是一个示例代码:

cs 复制代码
       public static T ShallowCopy<T>(this T original) where T : class
        {
            if (original == null)
            {
                return null;
            }

            // 创建一个新实例
            T copy = Activator.CreateInstance<T>();

            // 获取原始对象的所有属性
            var properties = typeof(T).GetProperties();

            foreach (var property in properties)
            {
                // 如果属性是一个引用类型或是List集合,进行浅拷贝
                if (property.PropertyType.IsClass && property.PropertyType != typeof(string)
                    || property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
                {
                    var originalValue = property.GetValue(original);

                    if (originalValue != null)
                    {
                        if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
                        {
                            // 如果属性是List集合,复制集合元素
                            var originalList = (System.Collections.IList)originalValue;
                            var copyList = (System.Collections.IList)Activator.CreateInstance(property.PropertyType);

                            foreach (var item in originalList)
                            {
                                copyList.Add(item);
                            }
                            property.SetValue(copy, copyList);

                             如果属性是List集合,复制集合元素
                            //var originalList = (System.Collections.IList)originalValue;
                            //var copyList = originalList.Cast<object>().ToList();

                            //property.SetValue(copy, copyList);
                        }
                        else
                        {
                            // 其他引用类型的属性,进行递归浅拷贝
                            var clonedObject = ShallowCopy(originalValue);
                            property.SetValue(copy, clonedObject);
                        }
                    }
                }
                else
                {
                    // 该属性是一个值类型,直接复制
                    var originalValue = property.GetValue(original);
                    property.SetValue(copy, originalValue);
                }
            }

            return copy;
        }

可以按照以下方式使用该扩展方法:

cs 复制代码
public class A
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

public class B
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

public class Program
{
    static void Main()
    {
        A a = new A { Foo = 42, Bar = "Hello" };
        B b = new B();

        b=a.ShallowCopy();

        Console.WriteLine($"a: Foo = {a.Foo}, Bar = {a.Bar}");
        Console.WriteLine($"b: Foo = {b.Foo}, Bar = {b.Bar}");
        
        b.Foo = 100; // 修改b对象的属性值
        
        Console.WriteLine($"a: Foo = {a.Foo}, Bar = {a.Bar}");
        Console.WriteLine($"b: Foo = {b.Foo}, Bar = {b.Bar}");
    }
}
相关推荐
snow@li10 分钟前
SpringBoot:AOP日志切面全景梳理/原理+流程+实战+避坑
java·开发语言·spring boot
Nil20811 分钟前
C++ emplace_back 与 push_back 详解
开发语言·c++
ma_king15 分钟前
Spring Boot 接入飞书自定义机器人
java·后端
2401_8949155315 分钟前
部署 GEO 优化源码常见报错排查:端口、伪静态、缓存问题解决
java·运维·服务器·后端·缓存·开源
七牛开发者28 分钟前
Codex 实践系列 Vol.04:用 Goal 和 Plan 管住一个长任务
java·数据库·人工智能·github·copilot
我命由我1234533 分钟前
Android Drawable - gradient
android·java·java-ee·kotlin·android studio·android-studio·android runtime
clear sky .34 分钟前
[freertos源码阅读]task.c任务篇
c语言·开发语言
XR1234567881 小时前
工业园区整体组网,方案对比怎么选?
开发语言·php
东东最爱敲键盘1 小时前
day9.C++多态之虚函数
开发语言·c++
码云数智-园园1 小时前
类似小鹅通的知识付费小程序平台有哪些
开发语言