Copy two identical object with different namespaces (recursive reflection)

后端 未结 7 1492
我寻月下人不归
我寻月下人不归 2021-01-06 19:22

I\'m working in c# with several workspaces that have one specific class which his always the same in each workspace. I would like to be able have a copy of this class to be

7条回答
  •  一个人的身影
    2021-01-06 20:01

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

    from here

提交回复
热议问题