How to reference a DLL at runtime?

I am building an application with WPF and C #, basically I want to allow anyone to create a dll and put it in a folder (similar to plugins). The application will take all the DLLs in the folder, load them and use their methods (defined on the interface).

Any ideas on linking to the dll at runtime? What is the best way to implement this?

+8
source share
6 answers

I implemented something like that you request this search through a dll in a given directory and discover classes that implement a specific interface. Below is the class I used to do this:

public class PlugInFactory<T> { public T CreatePlugin(string path) { foreach (string file in Directory.GetFiles(path, "*.dll")) { foreach (Type assemblyType in Assembly.LoadFrom(file).GetTypes()) { Type interfaceType = assemblyType.GetInterface(typeof(T).FullName); if (interfaceType != null) { return (T)Activator.CreateInstance(assemblyType); } } } return default(T); } } 

All you have to do is initialize this class like this:


  PlugInFactory<InterfaceToSearchFor> loader = new PlugInFactory<InterfaceToSearchFor>(); InterfaceToSearchFor instanceOfInterface = loader.CreatePlugin(AppDomain.CurrentDomain.BaseDirectory); 

If this answer or any other answer helps you solve your problem, mark it as the answer by clicking the checkmark. Also, if you feel that this is a good decision, support it to show your appreciation. Just thought that I mentioned this, since it does not seem like you accepted the answers to any of your other questions.

+10
source

Take a look at MEF . it should provide exactly what you are looking for.

Using reflection is also an option, but MEF would be a better choice if you ask me.

+1
source

I think you can start with something like

 Assembly assembly = Assembly.LoadFrom("Something.dll"); Type type = assembly.GetType("SomeType"); object instanceOfSomeType = Activator.CreateInstance(type); 

Then you can use it

+1
source
  string relative = "ClassLibrary1.dll"; string absolute = Path.GetFullPath(relative); Assembly assembly = Assembly.LoadFile(absolute); System.Type assemblytype = assembly.GetType("ClassLibrary1.Class1"); object []argtoppass={1,2}; var a =Activator.CreateInstance(assemblytype, argtoppass); System.Type type = a.GetType(); if (type != null) { string methodName = "add"; MethodInfo methodInfo = type.GetMethod(methodName); object result = methodInfo.Invoke(a, null); int a1 = (int )result; } 
0
source

I am working on something similar when the client may have different versions of a third-party DLL, although the name and location are the same. If we do not refer to the correct version, we will get crashes. I use reflection to identify the version, but I need to either change the setting to use a different DAL class depending on the version, or make an independent DAL version through the interface.

I am inclined to the second. If you create a “dummy DLL” in your code that implements an interface that has all the methods you want to call from the target assembly and pass not the actual type as a parameter, but an interface, then you should be able to use all the methods of your code development and compilation time based on your mock implementations and even do the testing, but then when you load the actual assembly at runtime, get the results from the real assembly. Let me know if this works for you, and I will also let you know.

Joe Morgan

0
source

The answers above pretty much give you what you need. You can try to download all the dlls that will be connected to your application when the program starts:

 // note: your path may differ, this one assumes plugins directory where exe is executed var pluginsDirectory = Path.Combine(AppContext.BaseDirectory, "plugins"); if (Directory.Exists(pluginsDirectory)) { var dllPaths = Directory.GetFiles(pluginsDirectory); if (dllPaths.Count() > 0) { foreach (var dllPath in dllPaths) { Assembly.LoadFrom(Path.Combine("plugins", Path.GetFileName(dllPath))); } } else { // warn about no dlls } } else { // warn about no plugins directory } 

How to refer to a DLL and its types:

 // if dll file name is My.Plugin.Assembly.ShortName.dll, then reference as follows var pluginAssembly = Assembly.Load("My.Plugin.Assembly.ShortName"); var typesInAssembly = pluginAssembly.GetExportedTypes(); var instanceType = typesInAssembly.FirstOrDefault(t => t.Name == "MyClassName"); var instance = Activator.CreateInstance(instanceType, new object[] { "constructorParam1", "constructorParam2" }); instanceType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, null, instance, new object[] { "methodParam1", "methodParam2" }); 

You may need to specify your application configuration in order to check the plugins directory. I assume that one child directory of plugins, you can check the list of subdirectory paths.

 ... <runtime> <assemblyBinding ... <probing privatePath="plugins" /> ... 

You need to say something, what type to implement (perhaps a configuration mapping). The interface, as far as I can tell, will simply provide a contract that all plugins will implement to provide the expected method for calling through reflection.

0
source

All Articles