C#-将对象强制转换到接口时出错

C#-将对象强制转换到接口时出错,c#,casting,C#,Casting,是的,有类似的问题,但没有一个解决了我的问题。我用三个项目创建了一个新的解决方案: FirstPlugin:库项目,编译为DLL MainApp:控制台应用程序将导入第一个插件 共享:声明接口的共享项目。FirstPlugin和MainApp项目的参考资料中都有该项目 共享项目 项目结构: ICrawler.cs代码: namespace Shared.Data.Structures { public interface ICrawler { void Sa

是的,有类似的问题,但没有一个解决了我的问题。我用三个项目创建了一个新的解决方案:

  • FirstPlugin:库项目,编译为DLL
  • MainApp:控制台应用程序将导入第一个插件
  • 共享:声明接口的共享项目。FirstPluginMainApp项目的参考资料中都有该项目
共享项目 项目结构:


ICrawler.cs代码:

namespace Shared.Data.Structures
{
    public interface ICrawler
    {
        void SayHello();
    }
}
FirstPlugin项目 项目结构:


FP.cs代码:

using System;
using Shared.Data.Structures;

namespace FirstPlugin
{
    public class FP : ICrawler
    {
        public void SayHello() {
            Console.WriteLine("Hello From FirstPlugin.dll");
        }
    }
}
MainApp项目 项目结构:


Program.cs代码:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using Shared.Data.Structures;

namespace MainApp
{
    class Program
    {
        static void Main(string[] args) {
            ICollection<ICrawler> plugins = GenericPluginLoader<ICrawler>.LoadPlugins(Directory.GetCurrentDirectory() + "\\modules");
            Console.ReadKey();
        }
    }

    public static class GenericPluginLoader<T>
    {
        public static ICollection<T> LoadPlugins(string path) {
            string[] dllFileNames = null;

            if (Directory.Exists(path)) {
                dllFileNames = Directory.GetFiles(path, "*.dll");

                ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
                foreach (string dllFile in dllFileNames) {
                    AssemblyName an = AssemblyName.GetAssemblyName(dllFile);
                    Assembly assembly = Assembly.Load(an);

                    assemblies.Add(assembly);
                }

                Type pluginType = typeof(T);
                ICollection<Type> pluginTypes = new List<Type>();
                foreach (Assembly assembly in assemblies) {
                    if (assembly != null) {
                        Type[] types = assembly.GetTypes();

                        foreach (Type type in types) {
                            if (type.IsInterface || type.IsAbstract) {
                                continue;
                            }
                            else {
                                if (type.GetInterface(pluginType.FullName) != null) {

                                    Console.WriteLine("ICrawler name: {0}", typeof(ICrawler).AssemblyQualifiedName);
                                    Console.WriteLine("Type name: {0}", type.GetInterface(pluginType.FullName).AssemblyQualifiedName);

                                    /*
                                        Output:

                                        ICrawler name: Shared.Data.Structures.ICrawler, MainApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
                                        Type name: Shared.Data.Structures.ICrawler, FirstPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
                                    */

                                    pluginTypes.Add(type);
                                }
                            }
                        }
                    }
                }

                ICollection<T> plugins = new List<T>(pluginTypes.Count);
                foreach (Type type in pluginTypes) {
                    T plugin = (T)Activator.CreateInstance(type);
                    plugins.Add(plugin);
                }

                return plugins;
            }

            return null;
        }
    }
}
我决定创建这个解决方案并复制粘贴确切的
GenericPluginLoader
源代码(从)

我正在处理的项目有不同的代码,但出现相同的错误

这个代码怎么了

我的生成输出:

  • D:\pluginest\modules\FirstPlugin.dll
  • D:\pluginest\MainApp.exe

英语不是我的母语,所以。。。你知道吗(╭☞ ͠° ͟ʖ °)╭☞.

共享项目将其源文件直接编译到引用它们的每个项目中

因此,您有两个
ICrawler
接口,每个程序集中一个接口,它们的类型不同(即使它们是相同的)

您试图将新实例强制转换为它未实现的接口副本;你不能那样做


您应该使用普通类库,而不是共享项目。

共享项目将其源文件直接编译到引用它们的每个项目中

因此,您有两个
ICrawler
接口,每个程序集中一个接口,它们的类型不同(即使它们是相同的)

您试图将新实例强制转换为它未实现的接口副本;你不能那样做


您应该使用普通类库,而不是共享项目。

检查在
Debug,Windows,Modules
中加载了多少个程序集副本。检查在
Debug,Windows,Modules
中加载了多少个程序集副本。
T plugin = (T)Activator.CreateInstance(type);