0

I have a class that I want to make a deep copy off, however it contains many attributes some of which contains other attributes. Now normally we can use either a clone or serialize method such as seen in How do you do a deep copy an object in .Net (C# specifically)? in order to get a good copy. However in my case several of these attributes come from a library which did not mark anything as either serializable or clonable.

Anybody know how to fix this? Something with reflection? Something efficient?

Also I know that parts of the program is going to be cyclicity linked and that there are private variables in the library.

icedwater
  • 4,701
  • 3
  • 35
  • 50
Thijser
  • 2,625
  • 1
  • 36
  • 71
  • I guess `Something with reflection` will do. Cache the `PropertyInfo`'s for speed. Or generate some IL code or use ExpressionsTrees for ultimate speed – Jeroen van Langen Aug 31 '16 at 12:36
  • Well my reflection skills aren't that great, so I know it should be able to deal with it somehow but how? – Thijser Aug 31 '16 at 12:38
  • There is no easy way unless you have any guaranteed restrictions on the data layout. Things like cyclic references are handled nicely by some serializers. Not every serializer requires the objects to be annotated with `[Serializable]` attribute. You could try `XamlServices.Save` / `XamlServices.Parse` for example but then you only copy the public interface with read/write properties. – grek40 Aug 31 '16 at 13:27
  • @grek40 Well there are private variables involved so that won't work. I do also know that there are cyclic references. – Thijser Aug 31 '16 at 13:47

0 Answers0