📜 ⬆️ ⬇️

Late binding in C #

Have you seen at least one C # project not using a DLL? A serious non-modular project? In any case, sooner or later for you this question will be critical. For the rest of this article is not of interest, because Here, only basic concepts with an example of the realization of reflection are described.

In .NET, writing plug-ins is a simple task that is solved with the help of reflection. Reflection allows you to dynamically load assemblies, get information about methods, properties, events, and class fields from assemblies, create new types, and call methods at run time. Classes and interfaces for reflection are in the System.Reflection namespace.
To begin with, we will write two dll-modules, in the classes of which we implement the interface common for each module and the main application.
The interface will be as follows:
namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

Interface.dll. . , , dll- Interdace.dll.
  1. namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

    Interface.dll. . , , dll- Interdace.dll.
  2. namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

    Interface.dll. . , , dll- Interdace.dll.
  3. namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

    Interface.dll. . , , dll- Interdace.dll.
  4. namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

    Interface.dll. . , , dll- Interdace.dll.
  5. namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

    Interface.dll. . , , dll- Interdace.dll.
  6. namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

    Interface.dll. . , , dll- Interdace.dll.
  7. namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

    Interface.dll. . , , dll- Interdace.dll.
  8. namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

    Interface.dll. . , , dll- Interdace.dll.
namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

Interface.dll. . , , dll- Interdace.dll.
namespace Interface { public interface Interface1 { int someMethod( int a, int b); string name { get ; } } } * This source code was highlighted with Source Code Highlighter .

Interface.dll. . , , dll- Interdace.dll.


Each module will assume an implementation of the method someMethod and the name property. The property will return the name of the module.
The class of each plug-in (module) will implement the interface from Interdace.dll, which must be added to the reference plug-ins. Developing a plugin, we have only the interface necessary for implementation.

Module 1
using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  1. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  2. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  3. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  4. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  5. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  6. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  7. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  8. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  9. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  10. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  11. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  12. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  13. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  14. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  15. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  16. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  17. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  18. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  19. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  20. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  21. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  22. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
  23. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .
using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin1 { public class pl1:Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a * b; } public string name { get { return pl_name; } } public static string getStaticString() { return "I'm a string from static method of plugin 1 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

')
Module 2
using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  1. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  2. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  3. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  4. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  5. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  6. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  7. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  8. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  9. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  10. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  11. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  12. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  13. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  14. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  15. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  16. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  17. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  18. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  19. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  20. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  21. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  22. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

  23. using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .

using System; using System.Collections. Generic ; using System.Text; using Interface; namespace Plugin2 { public class pl2 : Interface1 { private string pl_name = " " ; public int someMethod( int a, int b) { return a + b; } public string name { get { return pl_name; } } public string getNoneStaticString2() { return "I'm a string from none static method of plugin 2 which has been called using invoke method" ; } } } * This source code was highlighted with Source Code Highlighter .


So, the modules are ready. Let's start writing the main application MainApp. MainApp should track the appearance of new modules in a specific directory, the connection of those, the creation of objects of module classes and their conversion to the type of interface (also available in MainApp).
Search:
public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  1. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  2. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  3. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  4. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  5. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  6. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  7. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  8. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  9. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  10. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  11. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  12. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  13. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  14. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  15. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  16. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  17. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  18. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  19. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  20. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  21. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  22. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
  23. public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .
public void loadPlugins() { string [] files = Directory .GetFiles(folder, "*.dll" ); foreach ( string file in files) { try { Assembly assembly = Assembly .LoadFile(file); foreach (Type type in assembly.GetTypes()) { Type iface = type.GetInterface( "Interface.Interface1" ); if ( null != iface) { Interface.Interface1 obj = (Interface.Interface1)Activator.CreateInstance(type); plugins.Add(obj); } } } catch (Exception e) { } } } * This source code was highlighted with Source Code Highlighter .

Here the path to the directory with plugins is declared as follows:
private readonly string folder = Directory .GetCurrentDirectory() + "\\dlls\\"; * This source code was highlighted with Source Code Highlighter .
  1. private readonly string folder = Directory .GetCurrentDirectory() + "\\dlls\\"; * This source code was highlighted with Source Code Highlighter .
private readonly string folder = Directory .GetCurrentDirectory() + "\\dlls\\"; * This source code was highlighted with Source Code Highlighter .
private readonly string folder = Directory .GetCurrentDirectory() + "\\dlls\\"; * This source code was highlighted with Source Code Highlighter .

Using the Assembly class static LoadFile method, we loaded the assembly, passing the assembly name to the method. The Activator class is a special class for creating type instances and getting references to remote objects. Using the Activator class's CreateInstance method, we created a module class object, passing the type of the object being created to the method.
By calling loadPlugins () when starting the application, we dynamically load all the modules located in the dlls directory, in which the classes implement the necessary interface.

Then it's up to calling the methods. For any condition, select the module, which is going to call the method.
foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .
  1. foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .
  2. foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .
  3. foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .
  4. foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .
  5. foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .
  6. foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .
  7. foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .
foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .
foreach ( object pl in plugins) { if (((Interface.Interface1)pl).name.Equals(selected)) { result = ((Interface.Interface1)pl).someMethod(a, b); } } * This source code was highlighted with Source Code Highlighter .

Here the condition of choice was a combo box. Plugin1.pl1.someMethod () or Plugin2.pl2.someMethod () starts accordingly.

But what about static methods? The interface cannot help to work with them. We will do the Invoke () method, passing the name.
Assembly asm = Assembly .LoadFile(folder + "\\Plugin1.dll" ); Type classType = asm.GetType( "Plugin1.pl1" ); MethodInfo mi = classType.GetMethod( "getStaticString" ); label2.Visible = true ; label2.Text = mi.Invoke( null , null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  1. Assembly asm = Assembly .LoadFile(folder + "\\Plugin1.dll" ); Type classType = asm.GetType( "Plugin1.pl1" ); MethodInfo mi = classType.GetMethod( "getStaticString" ); label2.Visible = true ; label2.Text = mi.Invoke( null , null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  2. Assembly asm = Assembly .LoadFile(folder + "\\Plugin1.dll" ); Type classType = asm.GetType( "Plugin1.pl1" ); MethodInfo mi = classType.GetMethod( "getStaticString" ); label2.Visible = true ; label2.Text = mi.Invoke( null , null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  3. Assembly asm = Assembly .LoadFile(folder + "\\Plugin1.dll" ); Type classType = asm.GetType( "Plugin1.pl1" ); MethodInfo mi = classType.GetMethod( "getStaticString" ); label2.Visible = true ; label2.Text = mi.Invoke( null , null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  4. Assembly asm = Assembly .LoadFile(folder + "\\Plugin1.dll" ); Type classType = asm.GetType( "Plugin1.pl1" ); MethodInfo mi = classType.GetMethod( "getStaticString" ); label2.Visible = true ; label2.Text = mi.Invoke( null , null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  5. Assembly asm = Assembly .LoadFile(folder + "\\Plugin1.dll" ); Type classType = asm.GetType( "Plugin1.pl1" ); MethodInfo mi = classType.GetMethod( "getStaticString" ); label2.Visible = true ; label2.Text = mi.Invoke( null , null ).ToString(); * This source code was highlighted with Source Code Highlighter .
Assembly asm = Assembly .LoadFile(folder + "\\Plugin1.dll" ); Type classType = asm.GetType( "Plugin1.pl1" ); MethodInfo mi = classType.GetMethod( "getStaticString" ); label2.Visible = true ; label2.Text = mi.Invoke( null , null ).ToString(); * This source code was highlighted with Source Code Highlighter .
Assembly asm = Assembly .LoadFile(folder + "\\Plugin1.dll" ); Type classType = asm.GetType( "Plugin1.pl1" ); MethodInfo mi = classType.GetMethod( "getStaticString" ); label2.Visible = true ; label2.Text = mi.Invoke( null , null ).ToString(); * This source code was highlighted with Source Code Highlighter .

This will invoke the static method on the plugin_2 class. Notice that the first parameter of Invoke () is null.

In order to call a non-static method using Invoke, you only need to pass an object containing the method.
Assembly asm = Assembly .LoadFile(folder + "\\Plugin2.dll" ); Type classType = asm.GetType( "Plugin2.pl2" ); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod( "getNoneStaticString2" ); label3.Visible = true ; label3.Text = mi.Invoke(obj, null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  1. Assembly asm = Assembly .LoadFile(folder + "\\Plugin2.dll" ); Type classType = asm.GetType( "Plugin2.pl2" ); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod( "getNoneStaticString2" ); label3.Visible = true ; label3.Text = mi.Invoke(obj, null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  2. Assembly asm = Assembly .LoadFile(folder + "\\Plugin2.dll" ); Type classType = asm.GetType( "Plugin2.pl2" ); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod( "getNoneStaticString2" ); label3.Visible = true ; label3.Text = mi.Invoke(obj, null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  3. Assembly asm = Assembly .LoadFile(folder + "\\Plugin2.dll" ); Type classType = asm.GetType( "Plugin2.pl2" ); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod( "getNoneStaticString2" ); label3.Visible = true ; label3.Text = mi.Invoke(obj, null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  4. Assembly asm = Assembly .LoadFile(folder + "\\Plugin2.dll" ); Type classType = asm.GetType( "Plugin2.pl2" ); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod( "getNoneStaticString2" ); label3.Visible = true ; label3.Text = mi.Invoke(obj, null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  5. Assembly asm = Assembly .LoadFile(folder + "\\Plugin2.dll" ); Type classType = asm.GetType( "Plugin2.pl2" ); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod( "getNoneStaticString2" ); label3.Visible = true ; label3.Text = mi.Invoke(obj, null ).ToString(); * This source code was highlighted with Source Code Highlighter .
  6. Assembly asm = Assembly .LoadFile(folder + "\\Plugin2.dll" ); Type classType = asm.GetType( "Plugin2.pl2" ); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod( "getNoneStaticString2" ); label3.Visible = true ; label3.Text = mi.Invoke(obj, null ).ToString(); * This source code was highlighted with Source Code Highlighter .
Assembly asm = Assembly .LoadFile(folder + "\\Plugin2.dll" ); Type classType = asm.GetType( "Plugin2.pl2" ); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod( "getNoneStaticString2" ); label3.Visible = true ; label3.Text = mi.Invoke(obj, null ).ToString(); * This source code was highlighted with Source Code Highlighter .
Assembly asm = Assembly .LoadFile(folder + "\\Plugin2.dll" ); Type classType = asm.GetType( "Plugin2.pl2" ); object obj = Activator.CreateInstance(classType); MethodInfo mi = classType.GetMethod( "getNoneStaticString2" ); label3.Visible = true ; label3.Text = mi.Invoke(obj, null ).ToString(); * This source code was highlighted with Source Code Highlighter .

So, we dynamically loaded assemblies, created new types, and called methods (including static ones) at runtime — in a word, we tasted reflection.
The sources are here.

PS I really hope that this article will find its audience and be useful.

Source: https://habr.com/ru/post/88555/


All Articles