C# 中的深度克隆对象

发布于 2025-01-02 22:47:14 字数 707 浏览 2 评论 0原文

我在 C# 中尝试了不同的深度对象克隆技术,最终找到了非常优雅的解决方案,该解决方案使用反射并适用于不可序列化类型。我只是想知道它是否有问题,以及是否有人有不适用于此方法的评论或用例。这是代码。感谢您的评论!

    public static T Clone<T>(this T source)
    {
        //  Get the type 
        Type type = source.GetType();
        T clone = (T)Activator.CreateInstance(type);
        //  Loop through the properties
        foreach (PropertyInfo pInfo in type.GetProperties())
        {
            pInfo.SetValue(clone, pInfo.GetValue(source, null), null);
        }
        //  Loop through the fields
        foreach (FieldInfo fInfo in type.GetFields())
        {
            fInfo.SetValue(clone, fInfo.GetValue(source).Clone());
        }
        return clone;
    }

I was playing with different techniques for deep object cloning in C#, and finally came to pretty elegant solution that uses reflection and is applicable for non-serializable types. I am just wondering is there something wrong with it, and if anybody has comments or use case that does not work with this approach. Here is the code. Thanks for comments!

    public static T Clone<T>(this T source)
    {
        //  Get the type 
        Type type = source.GetType();
        T clone = (T)Activator.CreateInstance(type);
        //  Loop through the properties
        foreach (PropertyInfo pInfo in type.GetProperties())
        {
            pInfo.SetValue(clone, pInfo.GetValue(source, null), null);
        }
        //  Loop through the fields
        foreach (FieldInfo fInfo in type.GetFields())
        {
            fInfo.SetValue(clone, fInfo.GetValue(source).Clone());
        }
        return clone;
    }

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

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

发布评论

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

评论(2

何以心动 2025-01-09 22:47:14

我可以看到几个问题:

  1. 您只复制公共属性和字段
  2. 您不克隆属性值,您只需复制它。这意味着它不是属性的深度克隆。
  3. 有些类型不容易克隆(或根本不应该克隆),例如流或文件句柄(或任何与此相关的非托管资源) - 您的方法可能会遇到麻烦。
  4. 您的方法不会处理循环引用。
  5. 并非所有类型都有无参数构造函数。

There are several issues I can see:

  1. You only copy public properties and fields
  2. You don't clone the property value, you just copy it. This means it's not a deep clone on properties.
  3. There are certain types which are not easily clonable (or should not be cloned at all) like streams or filehandles (or any unmanaged resource for that matter) - your method might run into trouble there.
  4. Your method won't handle circular references.
  5. Not all types have a parameterless constructor.
_失温 2025-01-09 22:47:14

如果没有可访问的无参数构造函数怎么办?

如果存在无法共享的成员对象(可能是文件句柄)怎么办?

那么非公众成员呢?

为什么需要为一个不存在的问题创建一个通用的解决方案(您不需要能够深度克隆所有内容!)?

What if there is no accessible parameterless constructor?

What if there is an member object that can't be shared (a file handle, maybe)?

What about non-public members?

Why the need to create a one-size fits all solution to a problem that doesn't exist (you don't need to be able to deep clone everything!)?

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