Reputation: 133
Error info: System.InvalidCastException: Unable to cast object of type 'ClassLibrary1.Plugin' to type 'PluginInterface.IPlugin'.
What I'm trying to do is get my program to access an assembly and run whatever it may have. This loads the .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;
}
Both my program and my assembly have these two interfaces:
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);
}
}
My Class/Assembly to add:
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();
}
}
}
All help is greatly appreciated.
Upvotes: 2
Views: 1544
Reputation: 887547
Both my program and my assembly have these two interfaces:
There's your problem.
Two identical interfaces in two different assemblies create two distinct (and unrelated) types.
You need to define the interfaces in a single assembly and add a reference to it.
Upvotes: 5