在 C# 中克隆对象

发布于 2024-08-27 16:27:13 字数 785 浏览 9 评论 0原文

我用虚拟属性定义了下一个类:

public class Order: BaseEPharmObject
{
    public Order()
    {
    }

    public virtual Guid Id { get; set; }
    public virtual DateTime Created { get; set; }
    public virtual DateTime? Closed { get; set; }
    public virtual OrderResult OrderResult { get; set; }
    public virtual decimal Balance { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual Shift Shift { get; set; }
    public virtual Order LinkedOrder { get; set; }
    public virtual User CreatedBy { get; set; }
    public virtual decimal TotalPayable { get; set; }

    public virtual IList<Transaction> Transactions { get; set; }
    public virtual IList<Payment> Payments { get; set; }
}

并尝试克隆该派生类的对象。如何在基类中实现深拷贝?

I defined next class with virtual properties:

public class Order: BaseEPharmObject
{
    public Order()
    {
    }

    public virtual Guid Id { get; set; }
    public virtual DateTime Created { get; set; }
    public virtual DateTime? Closed { get; set; }
    public virtual OrderResult OrderResult { get; set; }
    public virtual decimal Balance { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual Shift Shift { get; set; }
    public virtual Order LinkedOrder { get; set; }
    public virtual User CreatedBy { get; set; }
    public virtual decimal TotalPayable { get; set; }

    public virtual IList<Transaction> Transactions { get; set; }
    public virtual IList<Payment> Payments { get; set; }
}

and trying to clone objects of that derived class. How to implement a deep copy right in the base class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久光 2024-09-03 16:27:13

如果您的类型是可序列化,您可以使用BinaryFormatter

public static T DeepClone<T>(T obj)
{
    using (var stream = new MemoryStream())
    {
        var formatter = new BinaryFormatter();
        formatter.Serialize(stream, obj);
        stream.Position = 0;
        return (T)formatter.Deserialize(stream);
    }
}

If your types are serializable you could use BinaryFormatter:

public static T DeepClone<T>(T obj)
{
    using (var stream = new MemoryStream())
    {
        var formatter = new BinaryFormatter();
        formatter.Serialize(stream, obj);
        stream.Position = 0;
        return (T)formatter.Deserialize(stream);
    }
}
尐籹人 2024-09-03 16:27:13

最好的方法通常是序列化实例并将其重新水合为新实例。 此处描述了执行此操作的一种方法。

我对本文的唯一警告是,不要将其实现为 ICloneable - 该接口已被弃用,并且会让类的调用者感到困惑。最好的办法是将此实现移至实用方法中并在那里调用它。

The best way is generally to serialize the instance and rehydrate it back as a new instance. One way of doing this is described here.

My only caveat to the article is that don't implement this as ICloneable - this interface is deprecated and is confusing to callers of your class. The best thing would be to move this implementation into a utility method and call it there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文