Ben

Ben

  • NA
  • 159
  • 0

Reflection and Interfaces (readable)

May 3 2007 1:09 AM
Hi all! Just a (hopefully) quick question.

I have a solution with the following projects included:

PluginInterface
In this project I have the following class (compiles as a C# library .dll)

PluginArchitecture.cs
-------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace PluginInterface
{
public interface PluginArchitecture
{
string pluginName();
string pluginDesc();
}
}
-------------------------

Next project is TestPlugin
Complies as a .dll The project references the PluginInterface project.

PluginInfo
-------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace TestPlugin
{
public class PluginInfo : PluginInterface.PluginArchitecture
{
public string pluginName()
{
return "Test Plugin";
}

public string pluginDesc()
{
return "This is a simple plugin that \n doesn't do anything";
}
}
}
-------------------------

Third project is MainModule and it references the PluginInterface project as well. The compiled .dll from project two is located in the bin\debug\Modules folder of the MainModule.
Here is the code I'm calling on the Form.Load() event for the form in the project (sorry for any line wrapping issues):

public void loadPlugins()
{
toolBar.Items.Clear();
foreach (string tmpFile in Directory.GetFiles(Application.StartupPath + @"\Modules"))
{
try
{
Assembly a = Assembly.LoadFrom(tmpFile);

object tmp = a.CreateInstance("TestPlugin.PluginInfo");
object tmp = a.CreateInstance("TestPlugin.PluginInfo");
PluginInterface.PluginArchitecture plug = (PluginInterface.PluginArchitecture)tmp;
//now that we have a plugin, create a button for it
ToolStripButton newButton = new ToolStripButton(plug.pluginName());
newButton.DisplayStyle = ToolStripItemDisplayStyle.Text;
toolBar.Items.Add(newButton);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}

This is the error message I get (the ex.Message)
"Unable to cast object of type 'TestPlugin.PluginInfo' to type 'PluginInterface.PluginArchitecture'."

Does anyone know why this is?? I would be very grateful for some assistance!! Thank you so much!!!