Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading DLLs at runtime in C#

Tags:

c#

reflection

dll

I am trying to figure out how you could go about importing and using a .dll at runtime inside a C# application. Using Assembly.LoadFile() I have managed to get my program to load the dll (this part is definitely working as I am able to get the name of the class with ToString()), however I am unable to use the 'Output' method from inside my console application. I am compiling the .dll then moving it into my console's project. Is there an extra step between CreateInstance and then being able to use the methods?

This is the class in my DLL:

namespace DLL {     using System;      public class Class1     {         public void Output(string s)         {             Console.WriteLine(s);         }     } } 

and here is the application I want to load the DLL

namespace ConsoleApplication1 {     using System;     using System.Reflection;      class Program     {         static void Main(string[] args)         {             var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");              foreach(Type type in DLL.GetExportedTypes())             {                 var c = Activator.CreateInstance(type);                 c.Output(@"Hello");             }              Console.ReadLine();         }     } } 
like image 676
danbroooks Avatar asked Aug 21 '13 16:08

danbroooks


People also ask

How do you call a DLL function in C#?

Add the DLL via the solution explorer - right click on references --> add reference then "Browse" to your DLL - then it should be available.

Can you write DLL in C#?

Calling methods and properties of a DLL from a C# client is also an easy task. Just follow these few simple steps and see how easy is to create and use a DLL in C#. Select File->New->Project->Visual C# Projects->Console Application.


2 Answers

Members must be resolvable at compile time to be called directly from C#. Otherwise you must use reflection or dynamic objects.

Reflection

namespace ConsoleApplication1 {     using System;     using System.Reflection;      class Program     {         static void Main(string[] args)         {             var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");              foreach(Type type in DLL.GetExportedTypes())             {                 var c = Activator.CreateInstance(type);                 type.InvokeMember("Output", BindingFlags.InvokeMethod, null, c, new object[] {@"Hello"});             }              Console.ReadLine();         }     } } 

Dynamic (.NET 4.0)

namespace ConsoleApplication1 {     using System;     using System.Reflection;      class Program     {         static void Main(string[] args)         {             var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");              foreach(Type type in DLL.GetExportedTypes())             {                 dynamic c = Activator.CreateInstance(type);                 c.Output(@"Hello");             }              Console.ReadLine();         }     } } 
like image 165
Dark Falcon Avatar answered Oct 05 '22 23:10

Dark Falcon


Right now, you're creating an instance of every type defined in the assembly. You only need to create a single instance of Class1 in order to call the method:

class Program {     static void Main(string[] args)     {         var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");          var theType = DLL.GetType("DLL.Class1");         var c = Activator.CreateInstance(theType);         var method = theType.GetMethod("Output");         method.Invoke(c, new object[]{@"Hello"});          Console.ReadLine();     } } 
like image 36
Reed Copsey Avatar answered Oct 05 '22 23:10

Reed Copsey