c# - Object deep copy in Silverlight -
i trying create copy of objects in silverligth 5 interfaces iformatters , iccloanble not support. *
my objects this: (note these obkjects obtained on deserializing xml): tried copy this:
[xmlroot(elementname = "component")] public class component { [xmlelement("attributes")] public attributes attributes { get; set; } [xmlignore] public attributes atrbtorginal = new attributes(); [xmlignore] public attributes atrbtcopy{ get; set; } } public component() { atrbtcopy= atrbtorginal ; }
sure not work got code on seraching on google :
public static class objectcopier { public static t clone<t>(t source) { if (!typeof(t).isserializable) { throw new argumentexception("the type must serializable.", "source"); } // don't serialize null object, return default 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); } } } , thought of doing liek this: objectorginal.clone();.
but problem in silverligth5 :
error 2 type or namespace name 'binaryformatter' not found (are missing using directive or assembly reference?) error 1 type or namespace name 'iformatter' not found (are missing using directive or assembly reference?)
is there alternative in silverlight 5 . please explain in detail. lot.
implement datacontractserializer attributes (datacontract, datamember) on classes , call datacontractserializer serialize memorystream, use again serialize out of memorystream new instance of object. far easiest understand, , quite performant too.
example of class definition :
[datacontract] class myclass { [datamember] public int myvalue {get;set;} [datamember] public string myothervalue {get;set;} }
the method of cloning 1 class instance covered in microsoft documentation http://msdn.microsoft.com/en-us/library/ms752244(v=vs.110).aspx
Comments
Post a Comment