I am trying some interresting features to load an assembly on the fly.
I read some samples, but what I am trying to do is a little more
complex.
- First, I have a app.config file containing keys like
"mydll_MyClass.dll"
- Second, I tried to load the assembly with Assembly.LoadFrom(myDll),
but since my constructor having some parameters this is not
possible.
- Third, I tried Activator.CreateInstance(...), but it becomes very
confusing to manage Types ... the examples I have seen use
Type.GetConstructor that I don't know how to use... To access this
function, I have to do Type.GetType(myDll).GetConstructor ( with type
here in params... don't know what type this time!!!)
So I tried the following code
// Getting the assembly to load... my own function, don't pay
attention
// here... the result put "mydll_MyClass.dll" in the string
assemblyList[0]
appConfig = new AppConfig.Config(ConfigurationSettings.AppSettings );
assemblyList = appConfig.GetAssemblyList();
System.Console.WriteLine("The assembly list: " + assemblyList[0]);
Assembly assembly = Assembly.LoadFrom(myPath + assemblyList[0]);
Type assemblyType = assembly.GetType(assemblyList[0]);
// The constructor name MyClass, that I want to someday extract from
// the name of mydll_MyClass.dll, but hardcoded here
// Here is the PROBLEM!, it returns null exception when finding
// constructor.
MethodInfo constructor =
assemblyType.GetMethod("MyClass");
Object obj = Activator.CreateInstance(assemblyType);
Object[] args = new Object[1];
// add a reference as target for the assembly to load and display
// my stuff, this is the param of my constructor
args[0] = this.Controls;
constructor.Invoke(obj, args);
Finally, if you have another solution I am open... :) My goal is to
have many .dll not referenced to my project, but load them via the
information that I can store in app.config file, and with params in
constructor of my assemblies class.
Thanks
*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*