xml - How to convert a ns1.MyPersonalClass into ns2.MyPersonalClass ? They have same property or members -
i have problem.
suppose have 2 class, have same name, same property, same members, etc. not same namespace. here's example:
namespace ns1 public class myownclass public member1 integer = 3 public member2 string = "noob" end class end namespace namespace ns2 public class myownclass public member1 integer = 0 public member2 string = "" end class end namespace
i want create function dynamically cast class namespace1 namespace2, , want function work on every class have same members.
how ?
i want can use this:
dim class1 new ns1.mypersonalclass dim class2 new ns2.mypersonalclass 'convert class1 class2 convert(class1, class1.gettype, class2, class2.gettype)
the equivalent in c# this
toutput convertequivalenttypes<tinput, toutput>(tinput structure) tinput : class toutput : class { toutput result = null; using (stream data = new memorystream()) { new xmlserializer(typeof(tinput)).serialize(data, structure); data.seek(0, seekorigin.begin); result = (toutput)new xmlserializer(typeof(toutput)).deserialize(data); } return result; }
vb.net equivalent of c# code have this:
imports system.io imports system.xml.serialization '... private function convertequivalenttypes(of tinput class, toutput class)([structure] tinput) toutput dim result toutput = nothing using data stream = new memorystream() dim oserializer new xmlserializer(gettype(tinput)) oserializer.serialize(data, [structure]) data.seek(0, seekorigin.begin) dim odeserializer new xmlserializer(gettype(toutput)) result = directcast(odeserializer.deserialize(data), toutput) end using return result end function
Comments
Post a Comment