G
Guest
Thanks to some great comments, I was able to get this code to work:
// Dynamically load the DLL
string sDLLName = "C:\\Test\\Test2.dll";
sDLLName = "..\\bin\\Test2.dll";
Assembly objAssembly = System.Reflection.Assembly.LoadFrom(sDLLName);
// Create an instance of the class
object testObject = objAssembly.CreateInstance("Test2.Test");
// Call the method "TestMessage" which has one argument
object[] object2 = new object[1];
object2[0] = 200;
MethodInfo Method = testObject.GetType().GetMethod("TestMessage");
object result = Method.Invoke(testObject, object2);
// This should return 300 and does
Console.WriteLine(result.ToString());
BUT reading a blog, there was an interesting comment made:
8. Reflection is slooooooooow
Relfection can be great if you use it with care. Late binding looks as it is
a holy grail for software extensibility (and yes it is, as I explained in a
post on my blog earlier). If you use late binding (e.g. when loading
"providers" from a configuration file using reflection), make sure you can
cache the retrieve object instance to avoid a second binding performance hit
(caused by Activator.CreateInstance for example).
My question is: how do you cache the object? Is that referring to the object
being “global†to the code so you are not loading it every time, or is there
a mechanism for handling caching of a late-bound DLL? I am trying to clarify
the comment.
Thanks so much!
Michael
// Dynamically load the DLL
string sDLLName = "C:\\Test\\Test2.dll";
sDLLName = "..\\bin\\Test2.dll";
Assembly objAssembly = System.Reflection.Assembly.LoadFrom(sDLLName);
// Create an instance of the class
object testObject = objAssembly.CreateInstance("Test2.Test");
// Call the method "TestMessage" which has one argument
object[] object2 = new object[1];
object2[0] = 200;
MethodInfo Method = testObject.GetType().GetMethod("TestMessage");
object result = Method.Invoke(testObject, object2);
// This should return 300 and does
Console.WriteLine(result.ToString());
BUT reading a blog, there was an interesting comment made:
8. Reflection is slooooooooow
Relfection can be great if you use it with care. Late binding looks as it is
a holy grail for software extensibility (and yes it is, as I explained in a
post on my blog earlier). If you use late binding (e.g. when loading
"providers" from a configuration file using reflection), make sure you can
cache the retrieve object instance to avoid a second binding performance hit
(caused by Activator.CreateInstance for example).
My question is: how do you cache the object? Is that referring to the object
being “global†to the code so you are not loading it every time, or is there
a mechanism for handling caching of a late-bound DLL? I am trying to clarify
the comment.
Thanks so much!
Michael