I have a DLL containing some methods (show, hide and validate). Here is an example of one of the methods hide(Panel paneldynamic, String id, List<EventActions> eventList)
. All methods contains the same parameters.
Now I have referenced the my DLL on my main form, how can I dynamically invoke one of the methods at runtime?
Introduction. A Dynamic Link library (DLL) is a library that contains functions and codes that can be used by more than one program at a time. Once we have created a DLL file, we can use it in many applications. The only thing we need to do is to add the reference/import the DLL File.
The use of DLLs helps promote modularization of code, code reuse, efficient memory usage, and reduced disk space. So, the operating system and the programs load faster, run faster, and take less disk space on the computer. When a program uses a DLL, an issue that is called dependency may cause the program not to run.
You'll need to use reflection. First, load the assembly (note that this assumes you've imported System.Reflection
):
Assembly a = Assembly.LoadFile(pathToTheDll);
Get the type containing the method by fully-qualified name:
Type t = a.GetType("Some.Class");
Now, get the method:
MethodInfo m = t.GetMethod("hide"); // For example
Then, all you have to do is invoke it:
m.Invoke(null, new object[] { /* parameters go here */ });
The first argument to Invoke
is the instance. If your class is static
, use null
, otherwise, you'll need to supply an instance of the type created using Assembly.CreateInstance
.
Insert your methods as delegates into a dictionary
public enum MethodType
{
None,
Show,
Hide,
Validate
}
var methods = new Dictionary<MethodType, Action<Panel, String, List<EventActions>>();
methods.Add(MethodType.Show, show);
methods.Add(MethodType.Hide, hide);
methods.Add(MethodType.Validate, validate);
Then you can invoke one of them with
MethodType methodToInvoke = MethodType.Hide;
methods[methodToInvoke](paneldynamic, id, eventList);
If you intend to dynamically load the DLL, this is another story. You will need at least three assemblies (three projects): one main assembly (exe), one contract assembly (dll) and one plug-in assembly (dll). The main and the plug-in assembly both have to reference the contract assembly. The contract assembly contains an interface
public interface IPlugIn
{
void Show(Panel paneldynamic, String id, List<EventActions> eventList);
void Hide(Panel paneldynamic, String id, List<EventActions> eventList);
void Validate(Panel paneldynamic, String id, List<EventActions> eventList);
}
The plug-in assembly contains a class implementing the interface
public class PlugIn : IPlugIn
{
// TODO: implement IPlugIn
}
In the main assembly you can load the plug-in like this
IPlugIn LoadPlugInFromFile(string fileName)
{
Assembly asm = Assembly.LoadFrom(fileName);
Type type = asm.GetType("PlugIn");
IPlugIn plugIn = (IPlugIn)Activator.CreateInstance(type);
return plugIn;
}
Invoke like this
IPlugIn plugIn = LoadPlugInFromFile("C:\PlugIns\MyPlugIn.dll");
plugIn.Show(paneldynamic, id, eventList);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With