问题 - 我正在尝试插件开发,除了尝试将对象 A 转换为对象 B 之外,一切进展顺利,即使 A 实现了 B。
界面:
namespace DynamicApplications
{
public interface IPlugIn
{
string Name { get; set; }
IPlugInHost myHost { set; }
void Show();
}
public interface IPlugInHost
{
bool Register(IPlugIn plug);
}
}
实现的类IPlugIn
namespace plugInOne
{
class PlugIn : IPlugIn
{
IPlugInHost _myHost;
public string Name
{
get
{
return "Plug-In One";
}
set
{
}
}
public IPlugInHost myHost
{
set
{
_myHost = value;
}
}
public void Show()
{
}
}
}
现在实例化的代码:
String path = Application.StartupPath;
string[] assemblyNames = Directory.GetFiles(path, "*.dll");
plugs = new IPlugIn[assemblyNames.Length];
for(int i = 0; i < assemblyNames.Length; i++)
{
string Name = assemblyNames[i];
Name = Name.Substring(Name.LastIndexOf("\\") + 1, Name.Length - Name.LastIndexOf("\\") - 1);
Name = Name.Remove(Name.LastIndexOf(".dll"));
assemblyNames[i] = Name;
}
for (int i = 0; i < assemblyNames.Length; i++)
{
Assembly DLL = Assembly.Load(assemblyNames[i]);
if(DLL != null)
{
try
{
Object p = Activator.CreateInstance(DLL.GetType(assemblyNames[i] + ".PlugIn"));
if (p is DynamicApplications.IPlugIn)
{
MessageBox.Show("YES!!!!");
}
else
{
MessageBox.Show("no>?>?>>><<?????");
}
plugs[i] = (IPlugIn)p;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
请注意,调试器显示p
实际上已实例化且可访问
应用程序总是命中MessageBox.Show("no>?>?>>><<?????");
请帮忙
艾登
编辑
P 的类型:
还
YET pluginOne.PlugIn 实现了 IPlugIn