繁体   English   中英

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

[英]C# - Error casting object to interface

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

  • FirstPlugin :库项目,编译为DLL。
  • MainApp :控制台应用程序,将导入FirstPlugin。
  • 共享 :声明接口的共享项目。 FirstPluginMainApp项目均在其引用上包含此项目。

共享项目

项目结构:

共享项目结构

ICrawler.cs代码:

namespace Shared.Data.Structures
{
    public interface ICrawler
    {
        void SayHello();
    }
}

FirstPlugin项目

项目结构:

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项目

项目结构:

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;
        }
    }
}

错误

我收到了这个漂亮的错误:

附加信息:无法将类型为“ FirstPlugin.FP”的对象转换为类型为“ Shared.Data.Structures.ICrawler”

在这一行(Program.cs):

T plugin = (T)Activator.CreateInstance(type);

我决定创建此解决方案并复制粘贴确切的GenericPluginLoader源(来自MSDN )。

我正在处理的项目具有不同的代码,但是发生相同的错误。

此代码有什么问题?

我的构建输出:

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM