Implementing PlugIn Function Using C #

I need to call a function to call a custom dll in C #.

For example, when a user creates abc.dll with an ABC class, I want to load the DLL to run the xyz (a) methods in the class.

object plugInObject = node.GetPlugInObject("abc.dll", "ABC"); plugInObject.runMethod("xyz", "a"); 

How to implement these functions in C #?

ADDED

This is the plugin code, and the dll is copied as plugin / plugin.dll.

 namespace HIR { public class PlugIn { public int Add(int x, int y) { return (x + y); } } } 

This is the one that calls this plugin.

 using System; using System.Reflection; class UsePlugIn { public static void Main() { Assembly asm = Assembly.LoadFile("./plugin/plugin.dll"); Type plugInType = asm.GetType("HIR.PlugIn"); Object plugInObj = Activator.CreateInstance(plugInType); var res = plugInType.GetMethod("Add").Invoke(plugInObj, new Object[] { 10, 20 }); Console.WriteLine(res); } } 
0
source share
2 answers

It can be translated into C # and .NET to the following:

 Assembly asm = Assembly.LoadFile("ABC.dll"); Type plugInType = asm.GetType("ABC"); Object plugInObj = Activator.CreateInstance(plugInType); plugInType.GetMethod("xyz").Invoke(plugInObj, new Object[] { "a" }); 

It is called Reflection .

+2
source
 * Declare the method with the static and extern C# keywords. * Attach the DllImport attribute to the method. The DllImport attribute allows you to specify the name of the DLL that contains the method. The common practice is to name the C# method the same as the exported method, but you can also use a different name for the C# method. * Optionally, specify custom marshaling information for the method parameters and return value, which will override the .NET Framework default marshaling. //Example of how to use methods from User32.dll [DllImport("User32.dll", SetLastError=true)] static extern Boolean MessageBeep(UInt32 beepType); 

see for more details

0
source

All Articles