.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.
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.
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.
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.
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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この記事が読者を見つけて、役に立つことを本当に願っています