0

first of all I'm a beginner in c#. I want to perform a deepcopy of a viewport class so I tried to copy items this way (like described on this post : here) :

 public static T Clone<T>(T source)
    {

        if (!typeof(T).IsSerializable)
        {
            throw new ArgumentException("The type must be serializable.", "source");
        }

        // Don't serialize a null object, simply return the default for that object
        if (Object.ReferenceEquals(source, null))
        {
            return default(T);
        }

        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream)
        {
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(stream);
        }
    }

but the class is not serializable and I don't own it. So I'm thinking about put an extension on the viewport class in order to add a method wich may copy variables content to the new instance but I'm not sure that's the good way.

Have you suggestions or other solutions ?

Thanks.

Community
  • 1
  • 1
taspai
  • 71
  • 6
  • 1
    There are many options [here](http://stackoverflow.com/questions/78536/deep-cloning-objects) and [here](http://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically). – dbc Jan 30 '16 at 18:41
  • 1
    Can you add `viewport` class definition? – Aleksandr Ivanov Jan 30 '16 at 18:42
  • 1
    @AleksandrIvanov you can find all the informations about viewport class here : [link](https://msdn.microsoft.com/fr-fr/library/system.windows.controls.viewport3d%28v=vs.110%29.aspx) – taspai Jan 30 '16 at 20:02

0 Answers0