是的,有类似的问题,但没有一个解决了我的问题。我创建了一个包含三个项目的新解决方案:
ICrawler.cs代码:
namespace Shared.Data.Structures
{
public interface ICrawler
{
void SayHello();
}
}
FP.cs代码:
using System;
using Shared.Data.Structures;
namespace FirstPlugin
{
public class FP : ICrawler
{
public void SayHello() {
Console.WriteLine("Hello From FirstPlugin.dll");
}
}
}
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)。
我正在处理的项目有不同的代码,但会出现同样的错误。
这段代码出了什么问题?
我的构建输出:
PS:英语不是我的母语,所以......你知道(╭͠͠°͟ʖ°)╭☞。
答案 0 :(得分:6)
共享项目将源文件直接编译到引用它们的每个项目中。
因此,您有两个ICrawler
接口,每个接头中有一个接口,它们的类型不同(即使它们相同)。
您正在尝试将新实例转换为它未实现的接口副本;你不能这样做。
您应该使用普通的类库,而不是共享项目。