c# - 带接口(interface)的插件架构;接口(interface)检查不起作用

标签 c# interface plugin-architecture

我正在应用程序中实现一个简单的插件架构。插件要求是使用应用程序和插件引用的 *.dll 中的接口(interface) (IPlugin) 定义的。该应用程序有一个插件管理器(也在同一个 *.dll 中),它通过查找插件文件夹中的所有 *.dll 来加载插件,加载它们,然后检查插件是否实现了接口(interface)。我已经通过两种不同的方式完成了这项检查[以前是通过一个简单的 if (plugin is IPlugin)],但是当插件实现接口(interface)时,没有人会识别。这是代码:

Assembly pluginAssembly = Assembly.LoadFrom(currFile.FullName);
if (pluginAssembly != null)
{
    foreach (Type currType in pluginAssembly.GetTypes())
    {
        if (currType.GetInterfaces().Contains(typeof(IPlugin)))
        {
            // Code here is never executing
            // even when the currType derives from IPlugin
        }
    }                    
}

我曾经测试过一个特定的类名(“插件”),但后来我让它循环遍历程序集中的所有类,但没有成功。 (这是我在别处找到的一个例子。) 为了使这更复杂一些,有两个接口(interface),每个接口(interface)都实现了原始接口(interface)(IPluginA、IPluginB)。该插件实际上实现了一个更具体的接口(interface) (IPluginB)。但是,我已经尝试使用仅实现更通用接口(interface) (IPlugin) 的插件,但仍然无法正常工作。

[编辑:回应我第一次收到的两个回复] 是的,我试过使用 IsAssignableFrom。请参阅以下内容:

Assembly pluginAssembly = Assembly.LoadFrom(currFile.FullName);
if (pluginAssembly != null)
{
    foreach (Type currType in pluginAssembly.GetTypes())
    {
        if (typeof(IPlugin).IsAssignableFrom(currType))
        {
            string test = "test";
        }
    }
}

最佳答案

你试过吗:

typeof(IPlugin).IsAssignableFrom(currType)

此外,类型实现接口(interface),但它们不是派生的。 BaseType属性(property)和IsSubclassOf方法显示推导,其中 IsAssignableFrom显示推导或实现。

编辑:您的程序集是否已签名?他们可能正在加载 side-by-side versions of your assembly ,并且由于 Type 对象与 ReferenceEquals 进行比较,因此两个并排程序集中的相同类型将完全独立。

编辑 2:试试这个:

public Type[] LoadPluginsInAssembly(Assembly otherAssembly)
{
    List<Type> pluginTypes = new List<Type>();
    foreach (Type type in otherAssembly.GetTypes())
    {
        // This is just a diagnostic. IsAssignableFrom is what you'll use once
        // you find the problem.
        Type otherInterfaceType =
            type.GetInterfaces()
            .Where(interfaceType => interfaceType.Name.Equals(typeof(IPlugin).Name, StringComparison.Ordinal)).FirstOrDefault();

        if (otherInterfaceType != null)
        {
            if (otherInterfaceType == typeof(IPlugin))
            {
                pluginTypes.Add(type);
            }
            else
            {
                Console.WriteLine("Duplicate IPlugin types found:");
                Console.WriteLine("  " + typeof(IPlugin).AssemblyQualifiedName);
                Console.WriteLine("  " + otherInterfaceType.AssemblyQualifiedName);
            }
        }
    }

    if (pluginTypes.Count == 0)
        return Type.EmptyTypes;

    return pluginTypes.ToArray();
}

关于c# - 带接口(interface)的插件架构;接口(interface)检查不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1269608/

相关文章:

java - 为什么监听器有时注册到 "this",有时注册到匿名类?

ruby-on-rails - 如何在 Ruby on Rails 中拥有插件架构?

Java : Override not needed methods inside classes - Update

java - 接口(interface)中的通用方法返回未检查的强制转换

c# - 在类库中获取 Razor 模板

c# - 没有创建 user.config 文件但 app.config 是?

c# - SQL Server CLR 和 T-SQL 存储过程与调用者是否相同?

c# - 无法更改 Xamarin 表单中 CollectionView 内框架的背景颜色

c# - 错误无法加载文件或程序集 'Interop.ActiveDs'