Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using Reflection
Message
From
20/12/2008 11:28:59
 
 
To
20/12/2008 11:14:27
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 3.0
OS:
Vista
Application:
Desktop
Miscellaneous
Thread ID:
01368964
Message ID:
01368966
Views:
21
Hi Bob,

Here's a little blurb I frequently use to show how to do this:

Code to instantiate a class with reflection:
string DLLName = "MyDLL.dll";
string ClassName = "MyDll.MyClass";
System.Reflection.Assembly oDLL;
object oClass;

// This "loads" the .DLL
oDLL = System.Reflection.Assembly.LoadFrom(DLLName);

// This creates the class instance
oClass = oDLL.CreateInstance(ClassName, true);
And if you need to pass parameters
// Create an object array to pass your parameters
object [] args = new object[1];
args[0] = MyKey;
oForm = (Form)oDLL.CreateInstance(classname, true, System.Reflection.BindingFlags.CreateInstance, null, args, null, null);
The only thing you'll need to change in the above sample, is that the DLLName will also include the path to the DLL. That should be all you need to do. (error trapping is recommended of course <g>)

~~Bonnie



>Can anyone point me in the right direction (sample code, etc.) to do the following:
>
>I need to invoke a class at runtime (most likely via reflection) contained in a DLL that I don't know until runtime its location. It will not be located in the start-in folder or program folder.
>
>Thanks!!
>
>Bob
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform