如何将包含嵌套Collections的Class复制到另一个Class中? [英] How to copy Class containing nested Collections into another Class?

查看:270
本文介绍了如何将包含嵌套Collections的Class复制到另一个Class中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我尝试使用反射将收集项目从一个类复制到另一个类.

I have tried to use reflection to copy collection items from one class to another.

但是还没有成功:(有没有一个Utility类或代码片段如何使用反射功能将嵌套Collection的项目复制到另一个Class Collection中?

But haven't succeeeded :( Has anyone a Utility class or code snippet how to copy nested Collection's items using reflection into another classes Collection?

谢谢

迈克

推荐答案

如果我能很好地理解您的问题,那么解决方案会比这容易得多.您只需要实现此示例所示的IClonable接口

the solution is easier than that if I well undertand your question. You just have to implement the IClonable Interface as this example shows


  class MyClass:ICloneable
  {
    public string MyProperty { get; set; }

    public object Clone()
    {
      return this;
    }
  }

//Somewhere in your code you close the staff and that's all

MyClass class1 = new MyClass { MyProperty = "hihi hahaha hohoho" };
MyClass class2 = class1.Clone() as MyClass;

Try to debug and compare the both classes


这篇关于如何将包含嵌套Collections的Class复制到另一个Class中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆