[英]Class which inherits from abstract class which implements interface, and loading this reflectively
I have an Interface named IPlugin. 我有一个名为IPlugin的接口。 This is implemented in an abstract class which is called TimerClass.
这是在称为TimerClass的抽象类中实现的。 There is then a class named Test which inherits from TimerClass.
然后有一个名为Test的类,该类继承自TimerClass。
The Test class is in it's own dll, and is loaded by reflection. Test类位于它自己的dll中,并通过反射加载。
This works perfectly well if i don't use the TimerClass (just implement IPlugin in Test). 如果我不使用TimerClass(仅在Test中实现IPlugin),则此方法效果很好。
My code which loads the Test class reflectively as an interface (IPlugin) looks like this: 我的代码反射性地将Test类作为接口(IPlugin)加载,如下所示:
private IPlugin GetInterface(Assembly assembly)
{
IPlugin PluginFound = null;
var iPluginType = typeof (IPlugin);
var types = assembly.GetExportedTypes();
foreach (var type in types)
if (iPluginType.IsAssignableFrom(type))
{
var operation = Activator.CreateInstance(type) as IPlugin;
if (operation != null)
{
PluginFound = operation;
break;
}
}
return PluginFound;
}
This part of the code: var operation = Activator.CreateInstance(type) as IPlugin;
这部分代码:
var operation = Activator.CreateInstance(type) as IPlugin;
crashes when the Test class is loaded. 加载Test类时崩溃。
At first i thought it could be that it tries to load the Abstract class TimerClass, but this seems far fetch since this class is not implemented in the same DLL. 起初我以为它可能试图加载Abstract类TimerClass,但这似乎很不容易,因为此类不在同一DLL中实现。 I also debugged it, and looked at the assembly, and to me it seems that the correct class is loaded...
我还调试了它,并查看了程序集,对我来说似乎已加载了正确的类...
Can anyone tell me why this is happening, and if there is a solution that i am not seeing? 谁能告诉我为什么会这样,是否有我看不到的解决方案?
This is embarresing.... 这真是令人难以置信。
The reason for it to fail was that the TimerClass had a property which defined the interval of the timer. 它失败的原因是TimerClass具有定义计时器间隔的属性。 This stops and starts the timer, so when this is called before the timer is created... well.... Then it crashes....
它将停止并启动计时器,因此在创建计时器之前调用此计时器...好吧...然后崩溃....
I am so sorry to have wasted your time, but thank you so much for helping me out! 很抱歉浪费您的时间,但是非常感谢您对我的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.