Tuesday, 15 May 2012

c# - Reflection Invoke Method -



c# - Reflection Invoke Method -

i have next c# code. base of operations class , classes inherit base of operations class. utilize baseclasses in special list. list has fellow member readlistasxmlas.

public class resultsetbase { members } public class resultsetbasesweep : resultsetbase { other members } public class resultlist<t> t : resultsetbase { public resultlist<t> readlistasxmlas(params string[] path) { ... } }

in other methode want create dynamic object of type resultlist. know of class resultlist is, @ runtime. (e.g. resulstsetbasesweep, or other inherited resultsetbase).

i create dynamic object of type. next way.

type mytype = type.gettype("class in string format"); type listtype = typeof(resultsaver.resultlist<>).makegenerictype(mytype); object resultlist = activator.createinstance(listtype);

now need phone call methode readlistasxmlas. of type object, compiler complains when when seek call

resultlist.readlistasxmlas(...);

so tried phone call on reflections:

mytype.invokemember("readlistasxmlas", bindingflags.invokemethod | bindingflags.instance | bindingflags.public, null, resultlist, new object[] { filenames.toarray() });

then compiler error: readlistasxmlas not found! how done right?

i have found solution problem:

type mytype = type.gettype(lstboxclass.selecteditem.tostring()); type listtype = typeof(resultsaver.resultlist<>).makegenerictype(mytype); object resultlist = activator.createinstance(listtype); methodinfo method = listtype.getmethod("readlistasxmlas"); method.invoke(resultlist, new object[] {filenames.toarray()});

c# generics reflection abstract

No comments:

Post a Comment