c# - Reflection usage for creating instance of a class into DLL -
i have following code:
var type = typeof(plugininterface.imbddxplugininterface); var types = appdomain.currentdomain.getassemblies().tolist() .selectmany(s => s.gettypes()) .where(p => type.isassignablefrom(p)); type t = types.elementat(0); plugininterface.imbddxplugininterface instance = activator.createinstance(t) plugininterface.imbddxplugininterface; tabpage tp = new tabpage(); tp = instance.plugintabpage();
the class within dll implements plugininterface , type in code above, definately correct class/type, when try create instance through interface error message saying:
object reference not assigned instance of object.
anybody know why?
thanks.
anyway
tabpage tp = new tabpage(); tp = instance.plugintabpage();
makes no sense.
do:
tabpage tp = instance.plugintabpage();
also next:
type type = appdomain.currentdomain.getassemblies() .selectmany(s => s.gettypes()) .firstordefault(p => type.isassignablefrom(p)); if (type != null) { // create instance }
or (my preferred way):
from asm in appdomain.currentdomain.getassemblies() type in asm.gettypes() !type.isinterface && !type.isabstract && typeof(itarget).isassignablefrom(type) select (itarget)activator.createinstance(type);
Comments
Post a Comment