c# - 为什么我的代码会抛出无效的转换异常? (C#)?

标签 c# dll .net-assembly .net

错误信息: System.InvalidCastException:无法将“ClassLibrary1.Plugin”类型的对象转换为“PluginInterface.IPlugin”类型。

我想做的是让我的程序访问程序集并运行它可能拥有的任何内容。 这会加载 .dll

private void AddPlugin(string FileName)
{
Assembly pluginAssembly = Assembly.LoadFrom(FileName);
foreach (Type pluginType in pluginAssembly.GetTypes())
{
if (pluginType.IsPublic)
{
if (!pluginType.IsAbstract)
{
Type typeInterface = pluginType.GetInterface("PluginInterface… true);
if (typeInterface != null)
{
Types.AvailablePlugin newPlugin = new Types.AvailablePlugin();
newPlugin.AssemblyPath = FileName;
newPlugin.Instance = (IPlugin)Activator.CreateInstance(plugin…
// Above line throws exception.

newPlugin.Instance.Initialize();
this.colAvailablePlugins.Add(newPlugin);
newPlugin = null;
}
typeInterface = null;
}
}
}
pluginAssembly = null;
}

我的程序和程序集都有这两个接口(interface):

using System;

namespace PluginInterface
{
public interface IPlugin
{
IPluginHost Host { get; set; }
string Name { get; }
string Description { get; }
string Author { get; }
string Version { get; }
System.Windows.Forms.Form MainInterface { get; }
void Initialize();
void Dispose();
void ReceivedMessage(PlayerIOClient.Message m);
void Disconnected();
}

public interface IPluginHost
{
void Say(string message);
void Send(PlayerIOClient.Message m);
void Send(string message_Type, params object[] paramss);
}
}

我要添加的类(class)/集会:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using PluginInterface;

namespace ClassLibrary1
{

public class Plugin : IPlugin // <-- See how we inherited the IPlugin interface?
{
public Plugin()
{

}

string myName = "Title";
string myDescription = "Descrip";
string myAuthor = "Me";
string myVersion = "0.9.5";


IPluginHost myHost = null;
Form1 myMainInterface = new Form1();



public string Description
{
get { return myDescription; }
}

public string Author
{
get { return myAuthor; }
}

public IPluginHost Host
{

get { return myHost; }
set { myHost = value; }
}

public string Name
{
get { return myName; }
}

public System.Windows.Forms.Form MainInterface
{
get { return myMainInterface; }
}

public string Version
{
get { return myVersion; }
}

public void Initialize()
{
//This is the first Function called by the host...
//Put anything needed to start with here first
MainInterface.Show();
}

public void ReceivedMessage(PlayerIOClient.Message m)
{

}
public void Disconnected()
{

}

public void Dispose()
{
MainInterface.Dispose();
}

}
}

非常感谢所有帮助。

最佳答案

Both my program and my assembly have these two interfaces:

这就是你的问题。

两个不同程序集中的两个相同接口(interface)创建两种不同(且不相关)的类型。

您需要在单个程序集中定义接口(interface)并添加对其的引用。

关于c# - 为什么我的代码会抛出无效的转换异常? (C#)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18195032/

相关文章:

c# - 如何防止重复项目listView C#

c# - 无法从 Visual Studio 2015 RC 中的 MySQL 数据库更新 EntityFramework 模型

matlab - 在 C DLL 中使用 delphi 数据类型的问题

c# - 嵌入式资源文件在运行时不存在 .Net 4

wpf - 相对于程序集的本地文件的 URI

c# - 如何正确显示启动画面

c# - Dictionary 是否损坏或 GetHashCode() 应该仅基于不可变成员?

c++ - 加载 DLL 是否会动态协调其 stderr 到主应用程序?如果是这样,那么如何......?

c# - 从 C# 调用带有 char* 的 C++ 函数

c# - 同一模块在 .net 核心上的 AppDomain 中加载了两次