c# - how to implement variable initialization when loading dll in net? -
made dynamic loading dll using reflection how implement variable initialization @ boot?
private bool loadplugins( string path ) { try { string[] pluginfiles = directory.getfiles(path, "*.dll"); plugins = new list<iplugin>(); ( int = 0; < pluginfiles.length; i++ ) { string args = pluginfiles[i].substring(pluginfiles[ ].lastindexof( "\\" ) + 1, pluginfiles[ ].indexof( ".dll" ) - pluginfiles[ ].lastindexof( "\\" ) - 1 ); type objtype = null; try { assembly ass = null; assemblyname myassemblyname = assemblyname.getassemblyname(pluginfiles[i].tostring()); ass = assembly.load(myassemblyname/*args*/); var s = ass.fullname; if ( ass != null ) { objtype = ass.gettype(args + ".plugin"); } } catch { continue; //если плагин корявый переходим к следующей итерации; } try { if ( objtype != null ) { plugins.add( (iplugin)activator.createinstance( objtype ) ); plugins[ plugins.count - 1 ].host = this; } } catch { return true; } }
i think confused between instance , assembly
. believe trying set value of variable in class in assembly. now, variables come existence when create instance of said class.
if take @ assembly.createinstance
method, allow set values well. however, these can constructor parameters. if these variables outside of constructor parameter, can make use of this:
assembly assembly = assembly.load("assemblyname"); type objecttype = assembly.gettype("typename"); object dynamicobject = activator.createinstance(objecttype); objecttype.getfield("variablename").setvalue(dynamicobject, "fieldvalue");
Comments
Post a Comment