.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}");
    }
}
相关推荐
不秃的开发媛1 分钟前
Java开发入门指南:IDE选择与数据库连接详解
java·数据库·ide
没有bug.的程序员8 分钟前
Redis Sentinel:高可用架构的守护者
java·redis·架构·sentinel
喵叔哟9 分钟前
58.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--图形验证码
微服务·架构·.net
Tiger_shl9 分钟前
【.Net技术栈梳理】08-控制反转(IoC)与依赖注入(DI)
开发语言·.net·.netcore
Zhen (Evan) Wang10 分钟前
.NET 6 文件下载
java·前端·.net
周杰伦fans10 分钟前
.NET 轻量级处理 Excel 文件库 - MiniExce
windows·.net·excel
Tiger_shl11 分钟前
【.Net技术栈梳理】10-.NET Core 程序的执行
开发语言·.net·.netcore
王嘉俊92523 分钟前
SpringBoot应用开发指南:从入门到高级配置与自动装配原理
java·spring boot·后端·spring·ssm
薄荷撞~可乐34 分钟前
C#高并发与并行理解处理
开发语言·c#