c# - Using reflection to serialize files -
in company's application, there's viewer take message , create xml document out of can presented nicely customers. each message has own class , far must individually written in examle below. class name given in "m_msgtypeversion", figured should possible use reflection generalize cases in switch statement doesn't have updated when new message types added application.
however, having bit of trouble doing this. don't know how cast specific type. using .net 3.5, dynamic keyword isn't there. need cast instance specific class instead of object.
and how use serializer? says needs t. type doesn't work this. gives syntax error.
switch (m_msgtypeversion) { default: throw new notimplementedexception("message type " + m_msgtypeversion + " conversion xmlelement not implemented!"); case "utilmd_d11aun51": utilmd_d11aun51 utilmd51 = new utilmd_d11aun51(); serializer<utilmd_d11aun51> serutilmd51 = new serializer<utilmd_d11aun51>(); if (serutilmd51.deserialize(m_edimsg, out utilmd51, out ex)) xml = new xmldocument().readnode(serutilmd51.serialize(utilmd51, "").root.createreader()) xmlelement; break; case "utilmd_d11aun51a": utilmd_d11aun51a utilmd51a = new utilmd_d11aun51a(); serializer<utilmd_d11aun51a> serutilmd51a = new serializer<utilmd_d11aun51a>(); if (serutilmd51a.deserialize(m_edimsg, out utilmd51a, out ex)) xml = new xmldocument().readnode(serutilmd51a.serialize(utilmd51a, "").root.createreader()) xmlelement; break; case ...
}
return xml;
my attempt....
type type = type.gettype("m_msgtypeversion"); object myobject = activator.createinstance(type); // need not object, cast specific type serializer<type> serutilmd51 = new serializer<type>(); // not work "type"
basically, you've got 3 choices here (in absence of dynamic); in reflection, hardcore meta-programming, or use single reflection hack jump generic method. latter best option, so:
step 1: create generic method want:
public static void evilhack<t>(...args...) { t myobject = activator.createinstance<t>(); serializer<t> serutilmd51 = new serializer<t>(); // etc }
step 2: call method unknown type via reflection
type type = type.gettype("m_msgtypeversion"); typeof(yourenclosingtype).getmethod("evilhack").makegenericmethod(type) .invoke(null, args);
note can add return type etc , catch return value .invoke
. leading null
in .invoke
call instance - null
in case because static
method; replace target instance if make method non-static. if make method non-public
, you'll need specify bindingflags
in getmethod
call.
fully working example:
using system; class yourenclosingtype { static void main() { type type = type.gettype("m_msgtypeversion"); object[] args = null; // no args typeof(yourenclosingtype).getmethod("evilhack").makegenericmethod(type) .invoke(null, args); } public static void evilhack<t>() { t myobject = activator.createinstance<t>(); serializer<t> serutilmd51 = new serializer<t>(); serutilmd51.doit(myobject); } } class m_msgtypeversion // worst. name. ever { } class serializer<t> { public void doit(t obj) { console.writeline("i'm serializing " + obj); } }
Comments
Post a Comment