C# WPF中实现深拷贝的五种方式

1. 手动实现深拷贝

代码示例:

复制代码
cs 复制代码
public class Person
{
    public string Name { get; set; }
    public Address Address { get; set; }
}


public class Address
{
    public string City { get; set; }
    public string Street { get; set; }
}


public class PersonDeepCopier
{
    public static Person DeepCopy(Person original)
    {
        return new Person
        {
            Name = original.Name,
            Address = new Address
            {
                City = original.Address.City,
                Street = original.Address.Street
            }
        };
    }
}

优点:

  • 完全控制拷贝过程。

  • 可以定制化处理特殊成员。

缺点:

  • 代码冗长,尤其是对象结构复杂时。

  • 容易出错,需要手动更新所有新成员。

使用场景:

  • 当对象结构简单且不经常改变时。

  • 当需要对拷贝过程进行精细控制时。

2. 使用序列化

代码示例:

复制代码
cs 复制代码
[Serializable]
public class Person
{
    public string Name { get; set; }
    public Address Address { get; set; }
}


public class Address
{
    public string City { get; set; }
    public string Street { get; set; }
}


public static class PersonDeepCopier
{
    public static Person DeepCopy(Person original)
    {
        using (var ms = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(ms, original);
            ms.Position = 0;
            return (Person)formatter.Deserialize(ms);
        }
    }
}

优点:

  • 自动处理所有可序列化的成员。

  • 相对简洁的代码。

缺点:

  • 需要所有成员都是可序列化的。

  • 性能开销较大,尤其是在大型对象上。

  • 安全性问题,因为序列化可能会暴露敏感信息。

使用场景:

  • 当对象需要持久化或网络传输时。

  • 当对象结构复杂且成员都是可序列化时。

3. 使用表达式克隆

代码示例:

复制代码
cs 复制代码
public static class PersonDeepCopier
{
    public static Person DeepCopy(Expression<Func<Person, Person>> materializer)
    {
        var original = new Person { Name = "John", Address = new Address { City = "New York", Street = "5th Avenue" } };
        var copy = materializer.Compile().Invoke(original);
        return copy;
    }
}

优点:

  • 利用表达式树可以动态生成拷贝逻辑。

  • 代码简洁,易于理解。

缺点:

  • 需要对LINQ和表达式树有一定的了解。

  • 性能可能不如手动实现。

使用场景:

  • 当需要动态生成拷贝逻辑时。

  • 当对象结构相对固定且需要快速实现深拷贝时。

4. 使用第三方库

代码示例:

复制代码
cs 复制代码
// 假设使用了一个名为DeepCloner的第三方库
public static class PersonDeepCopier
{
    public static Person DeepCopy(Person original)
    {
        return DeepCloner.Clone(original);
    }
}

优点:

  • 简单易用,一行代码实现深拷贝。

  • 通常经过优化,性能较好。

缺点:

  • 需要引入外部依赖。

  • 可能需要购买许可证。

使用场景:

  • 当项目中需要频繁进行深拷贝操作时。

  • 当需要快速实现深拷贝且不关心引入外部依赖时。

5. 使用ICloneable接口

代码示例:

复制代码
cs 复制代码
public class Person : ICloneable
{
    public string Name { get; set; }
    public Address Address { get; set; }


    public object Clone()
    {
        return this.MemberwiseClone();
    }
}


public class Address : ICloneable
{
    public string City { get; set; }
    public string Street { get; set; }


    public object Clone()
    {
        return this.MemberwiseClone();
    }
}


public static class PersonDeepCopier
{
    public static Person DeepCopy(Person original)
    {
        var clone = (Person)original.Clone();
        clone.Address = (Address)((Address)original.Address).Clone();
        return clone;
    }
}

优点:

  • 遵循了.NET框架的设计模式。

  • 可以定制化处理特殊成员。

缺点:

  • 需要手动实现每个类的克隆逻辑。

  • 需要记住实现接口。

使用场景:

  • 当需要遵循.NET框架的设计模式时。

  • 当对象结构简单且需要手动控制拷贝过程时。

总结

深拷贝在C# WPF应用程序中是一个重要的概念,有多种方式可以实现。手动实现深拷贝提供了最大的灵活性,但代码量较大;序列化方法简单但性能开销大;表达式克隆和第三方库提供了简洁的解决方案,但可能需要额外的学习成本或依赖;ICloneable接口遵循了.NET的设计模式,但需要手动实现每个类的克隆逻辑。每种方法都有其适用场景,开发者应根据具体需求和项目情况选择合适的实现方式。

相关推荐
hez20103 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
雨落倾城夏未凉9 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫10 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫11 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m62511 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户917215619021111 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠11 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫13 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech14 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf15 天前
C#摸鱼实录——IoC与DI案例详解
c#