简体   繁体   English

从实现接口的抽象类继承的类,并进行反射性加载

[英]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.

相关问题 从抽象类继承并实现接口的模拟类 - Mock class which inherit from abstract class and implements interface 对实现通用接口的抽象类执行“保护的抽象替代” - Perform a 'protected abstract override' for an abstract class which implements a generic interface 从抽象类继承的类中的泛型列表上的Foreach循环 - Foreach loop on a generic list in a class which inherits from a abstract class 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) 从它实现的接口创建 class 的实例 - Create Instance of a class from interface which it implements 在运行时创建类型,继承抽象类并实现接口 - Create type at runtime that inherits an abstract class and implements an interface 取实现接口的类 - Take class which implements interface 具体类继承自抽象类,抽象类继承自通用抽象类 - Concrete Class inherit from Abstract class which inherits from Generic Abstract Class 将参数传递给继承接口的类 - Pass parameter to class which inherits an interface 犀牛模拟接口IA,该接口使用抽象类aB实现接口IB - rhino mock an interface IA which implements interface IB with abstract class aB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM