Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamicly load DLL.

Status
Not open for further replies.

flnhst

Programmer
Aug 9, 2005
12
NL
how do i dynamicly load a user input given DLL in C#? The DLL is also written in C# and .NET. I have googled around, but i couldnt find anything.
 
Look up reflection in msdn. I think the method you are looking for is called LoadAssemblyAndUnWrap or something similar.

HTH

Smeat
 
There's 4 methods you can use on the Assembly object:
Load()
LoadFile()
LoadFrom()
LoadWithPartialName()

Once you've loaded it, you would then call GetMethods on it to get a list of methods, and then call MethodBase.Invoke on the method you want to call.

You'll need a using statement for System.Reflection to do most of this.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Just dug out this sample from one of my previous projects, may be of some use:

Be sure to include the reflection assembly:

Code:
using System.Reflection;

Code:
try
{						
  //Load Diary assembly
  Assembly assembly = Assembly.LoadFrom(System.AppDomain.CurrentDomain.BaseDirectory + "Bin\\ShowroomSuite.Library.Diary.dll");
							   
								// Walk through each type in the assembly
								foreach (Type type in assembly.GetTypes ()) 
								{							if (type.IsClass == true && type.FullName == "MyNamespace.MyClassName") 
									{
										// Create parameter list
										object[] arguments = new object [] {Dr};
										objAppointmentList = type.InvokeMember("MyMethodName", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, arguments);
									}
								}
							}
							catch (Exception ex)
							{
								//Code ommitted for clarity 
							}
							finally
							{
//Code ommitted for clarity 
							}

HTH

Smeat
 
Thanks! It worked.

Here is the code that i used:

Code:
static void Main(string[] args)
		{
			Assembly T = Assembly.LoadFrom("CSDLL.dll");

			foreach ( Type A in T.GetTypes() )
			{
				if (A.IsClass && A.Name == "CSDLL")
				{
					Console.WriteLine("Found. Invoking Function.");

					object[] Parameters = new Object[2];

					Parameters[0] = 2;
					Parameters[1] = 5;

					A.GetMethod("TestFunction").Invoke(null, Parameters);
				}
				else
				{
					Console.WriteLine("Class not found.");
					Console.WriteLine(A.Name);
					Console.WriteLine(Convert.ToString(A.IsClass));
				}
			}

			Console.ReadLine();
		}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top