和
public abstract class CompositionPlugin { ... }
和
public class MyCompositionPlugin : CompositionPlugin { ... }
我想检查一个对象的类型是否等于给定的类型:
public class Framework {
public IList<CompositionPlugin> CompositionPlugins = new List<CompositionPlugin>();
public CompositionPlugin GetCompositionPlugin(Type ofType)
{
foreach (CompositionPlugin plugin in CompositionPlugins)
{
if (plugin.GetType().Equals(ofType))
return plugin;
}
throw new ArgumentException("A composition plugin of type " + ofType.FullName + " could not be found");
}
}
该方法的调用方式如下:
Framework framework = new Framework();
// Adding the plugin to the framework is done by loading an assembly and
// checking if it contains plugin-compatible classes (i.e. subclasses
// of <CompositionPlugin>)
framework.RegisterAssembly("E:\\Projects\\Framework\\MyPlugin\\bin\\Debug\\MyPlugin.dll");
CompositionPlugin plugin = framework.GetCompositionPlugin(typeof(MyCompositionPlugin));
然而,在测试时,这个检查总是失败,即使我在我请求的列表中肯定有那种类型的对象。
据我了解,它应该返回在-ListMyCompositionPlugin
中找到的第一个实例。CompositionPlugins
我的类型检查错了吗?为什么?它是如何正确完成的?