C#の遅延バインディング

DLLを使用していない少なくとも1つのC#プロジェクトを見ましたか? 重大な非モジュールプロジェクトですか? いずれにせよ、遅かれ早かれ、この問題はあなたにとって重要になります。 残りについては、この記事は興味がありません。なぜなら、 ここでは、リフレクションの実装例とともに基本的な概念のみを説明します。



.NETでは、プラグインの作成はリフレクションを使用して実行できる簡単なタスクです。 Reflectionでは、アセンブリを動的にロードし、アセンブリからクラスのメソッド、プロパティ、イベント、フィールドに関する情報を取得し、実行時に新しいタイプを作成し、メソッドを呼び出すことができます。 リフレクションのクラスとインターフェイスは、System.Reflection名前空間にあります。

まず、2つの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.









  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.









各モジュールは、someMethodメソッドの実装とnameプロパティを想定しています。 プロパティはモジュールの名前を返します。

各プラグイン(モジュール)のクラスは、プラグイン参照に追加する必要があるInterdace.dllからのインターフェイスを実装します。 プラグインを開発するとき、実装に必要なインターフェースのみがあります。



モジュール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 .













モジュール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 .













これでモジュールの準備ができました。 メインのMainAppアプリケーションの作成を始めましょう。 MainAppは、特定のディレクトリ内の新しいモジュールの外観、それらの接続、モジュールクラスオブジェクトの作成、およびインターフェイスの種類への変換(MainAppでも利用可能)を追跡する必要があります。

検索:

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 .











ここで、プラグインのあるディレクトリへのパスは次のように宣言されます:

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 .











Assemblyクラスの静的LoadFileメソッドを使用して、アセンブリ名をメソッドに渡すことでアセンブリをロードしました。 Activatorクラスは、型をインスタンス化し、リモートオブジェクトへの参照を取得するための特別なクラスです。 ActivatorクラスのCreateInstanceメソッドを使用して、作成したオブジェクトのクラスタイプをメソッドに渡すことにより、モジュールクラスオブジェクトを作成しました。

アプリケーションの起動時にloadPlugins()を呼び出すことにより、dllsディレクトリにあるすべてのモジュールを動的にロードします。このディレクトリでは、クラスが目的のインターフェースを実装します。



次は、メソッドの呼び出しです。 何らかの条件で、メソッドを呼び出すモジュールを選択します。

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 .











ここでは、選択条件はコンボボックスでした。 したがって、Plugin1.pl1.someMethod()またはPlugin2.pl2.someMethod()が起動します。



しかし、静的メソッドはどうでしょうか? インターフェイスはそれらを使用するのに役立ちません。 Invoke()メソッドを実行して、名前を渡します。

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 .











これにより、plugin_2クラスの静的メソッドが呼び出されます。 Invoke()の最初のパラメーターとしてnullを渡したことに注意してください。



Invokeを使用して非静的メソッドを呼び出すには、メソッドを含むオブジェクトを渡すだけです。

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 .











そのため、実行時にアセンブリを動的にロードし、新しい型を作成し、実行時にメソッド(静的なものも含む)を呼び出しました。

ソースはこちら



PSこの記事が読者を見つけて、役に立つことを本当に願っています




All Articles