C # Using assembly to call a method in a DLL

I read a lot about this - it seems to me that I am very close to the answer. I'm just looking for a method call from the DLL file that I created.

For instance:

My dll file:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExampleDLL { class Program { static void Main(string[] args) { System.Windows.Forms.MessageBox.Show(args[0]); } public void myVoid(string foo) { System.Windows.Forms.MessageBox.Show(foo); } } } 


My expression:

 string filename = @"C:\Test.dll"; Assembly SampleAssembly; SampleAssembly = Assembly.LoadFrom(filename); // Obtain a reference to a method known to exist in assembly. MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("myVoid"); // Obtain a reference to the parameters collection of the MethodInfo instance. 

All credits go to SO user 'woohoo' for the above snippet. How to call a managed DLL file in C #?

Now, however, I would like to be able to not only refer to my Dll (and the methods inside it), but correctly call the methods inside it (in this case, I would like to call the myVoid method).

Anyone have any suggestions for me?

Thanks,

Evan

+4
source share
4 answers

The question and answer that you are referencing uses reflection to invoke a method in a managed DLL. This is not necessary if, as you say, you want to do this, you simply reference your DLL. Add the link (using the Add Link option in Visual Studio), and you can call your method directly like this:

 ExampleDLL.Program p = new ExampleDLL.Program(); // get an instance of `Program` p.myVoid(); // call the method `myVoid` 

If you want to go through the reflection route (as indicated by woohoo ), you still need an instance of your Program class.

 Assembly SampleAssembly = Assembly.LoadFrom(filename); Type myType = SampleAssembly.GetTypes()[0]; MethodInfo Method = myType.GetMethod("myVoid"); object myInstance = Activator.CreateInstance(myType); Method.Invoke(myInstance, null); 

Now you have an instance of Program and you can call myVoid .

+6
source
 //Assembly1.dll using System; using System.Reflection; namespace TestAssembly { public class Main { public void Run(string parameters) { // Do something... } public void TestNoParameters() { // Do something... } } } //Executing Assembly.exe public class TestReflection { public void Test(string methodName) { Assembly assembly = Assembly.LoadFile("...Assembly1.dll"); Type type = assembly.GetType("TestAssembly.Main"); if (type != null) { MethodInfo methodInfo = type.GetMethod(methodName); if (methodInfo != null) { object result = null; ParameterInfo[] parameters = methodInfo.GetParameters(); object classInstance = Activator.CreateInstance(type, null); if (parameters.Length == 0) { result = methodInfo.Invoke(classInstance, null); } else { object[] parametersArray = new object[] { "Hello" }; result = methodInfo.Invoke(classInstance, parametersArray); } } } } } 
+2
source

Just add this line of code after receiving the link to your method.

  Method.Invoke(classInstance, new object[] {}); 

I hope for this help.

0
source

MethodInfo has an Invoke method. So just call Method.Invoke() with the object you created, for example, call System.Activator.CreateInstance()

0
source

All Articles