Loading DLL files at runtime in C #

I am trying to figure out how you can import and use DLLs at runtime inside a C # application. Using Assembly.LoadFile (), I was able to load my program to load the dll (this part definitely works, since I can get the class name with ToString ()), however I cannot use the 'Output' method from my console application. I compile .dll and move it to my console project. Is there an additional step between CreateInstance and the ability to use 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 that I want to download 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(); } } } 
+42
reflection c # dll
Aug 21 '13 at 16:02
source share
5 answers

Members must be allowed at compile time, which must be called directly from C #. Otherwise, you should 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(); } } } 
+67
Aug 21 '13 at 16:05
source share

You are currently creating an instance of each type defined in the assembly. You only need to create one instance of Class1 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(); } } 
+22
Aug 21 '13 at 16:08
source share

You need to create an instance of a type that sets the Output method:

 static void Main(string[] args) { var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll"); var class1Type = DLL.GetType("DLL.Class1"); //Now you can use reflection or dynamic to call the method. I will show you the dynamic way dynamic c = Activator.CreateInstance(class1Type); c.Output(@"Hello"); Console.ReadLine(); } 
+6
Aug 21 '13 at 16:11
source share

Activator.CreateInstance() returns an object that does not have an output method.

Sounds like you came from dynamic programming languages? C # is definitely not the same, and what you are trying to do will be difficult.

Since you are loading a specific DLL from a specific location, maybe you just want to add it as a link to a console application?

If you absolutely want to load the assembly via Assembly.Load , you will need to go through the reflection to call any members on c

Something like type.GetMethod("Output").Invoke(c, null); must do it.

0
Aug 21 '13 at 16:13
source share

It is not that difficult.

You can check the available functions of the loaded object, and if you find the one you are looking for by name, then look at its expected parts, if any. If this is the call you are trying to find, call it using the Invoke method of the MethodInfo object.

Another option is to simply create external objects for the interface and transfer the loaded object to this interface. If successful, call the function initially.

This is pretty simple stuff.

-5
Oct 06 '14 at 14:29
source share



All Articles