プラグインシステムをC#で実装しようとしています。そのために、次のクラスとインターフェイスを作成しました。
ローダーとプラグインの両方に含まれています:
interface IDevicePlugin {
string GetName();
string GetVersion();
}
プラグインコード(.dllにコンパイル)
public class DummyPlugin : IDevicePlugin {
protected string name;
protected string version;
public string GetName() {
return name;
}
public string GetVersion() {
return version;
}
}
プラグインをロードするコードは次のとおりです。
IDevicePlugin thePlugin;
Assembly plugin = Assembly.LoadFrom("plugin.dll");
foreach (Type pluginType in plugin.GetTypes()) {
if (pluginType.IsPublic && !pluginType.IsAbstract) {
Type typeInterface = pluginType.GetInterface("IDevicePlugin", true);
if (typeInterface != null) {
// the plugin implements our IDevicePlugin interface
thePlugin = (IDevicePlugin)Activator.
CreateInstance(plugin.GetType(pluginType.ToString()));
}
}
}
そして、これは次のようにクラッシュします:
Unable to cast object of type 'PluginTest.DummyPlugin' to type 'PluginTest.IDevicePlugin'.