Nie wiem co się dzieje... Tworzę sobie aplikację z obsługą wtyczek na bazie pewnego tutoriala. W nim wtyczka musi implementować interfejs IPlugin, u mnie się to nazywa IThetysPlugin.
Dodawanie wtyczki i jej ładowanie wygląda tak:
//Create a new assembly from the plugin file we're adding..
Assembly pluginAssembly = Assembly.LoadFrom(FileName);
//Next we'll loop through all the Types found in the assembly
foreach (Type pluginType in pluginAssembly.GetTypes())
{
if (pluginType.IsPublic) //Only look at public types
{
if (!pluginType.IsAbstract) //Only look at non-abstract types
{
//Gets a type object of the interface we need the plugins to match
Type typeInterface = pluginType.GetInterface("IThetysPlugin", true);
//Make sure the interface we want to use actually exists
if (typeInterface != null)
{
//Create a new available plugin since the type implements the IPlugin interface
Types.AvailablePlugin newPlugin = new Types.AvailablePlugin();
//Set the filename where we found it
newPlugin.AssemblyPath = FileName;
//Create a new instance and store the instance in the collection for later use
//We could change this later on to not load an instance.. we have 2 options
//1- Make one instance, and use it whenever we need it.. it's always there
//2- Don't make an instance, and instead make an instance whenever we use it, then close it
//For now we'll just make an instance of all the plugins
newPlugin.Instance = (IThetysPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
//Set the Plugin's host to this class which inherited IPluginHost
newPlugin.Instance.Host = this;
(...)
Wywraca się na rzutowaniu Activator.CreateInstance na IThetysPlugin twierdząc, że "Unable to cast". Czyżby nie dało się rzutować klasy implementującej interfejs na tenże interfejs? Dlaczego?
Sama wtyczka, którą skompilowaną do postaci DLL próbuję załadować wygląda tak:
namespace ClassicSkinLoader
{
public class ClassicSkinLoader : IThetysPlugin
{
public ClassicSkinLoader()
{
}
//Declarations of all our internal plugin variables
string myName = "Classic Skin Loader Plugin";
string myDescription = "Wtyczka do ładowania klasycznych skórek programu PilotMP3";
string myAuthor = "Marcin \"Ktos \" Badurowicz";
string myVersion = "1.0.0";
IPluginHost myHost = null;
System.Windows.Forms.UserControl myMainInterface = new Cocpit();
/// <summary>
/// Description of the Plugin's purpose
/// </summary>
public string Description
{
get {return myDescription;}
}
/// <summary>
/// Author of the plugin
/// </summary>
public string Author
{
get {return myAuthor;}
}
(... tutaj reszta rzeczy, które musi implementować coś co dziedziczy po IThetysPlugin)
Ktoś ma jakiś pomysł dlaczego to nie działa? Większość rzeczy kopiowałem ze wspomnianego tutoriala, jedynie co najwyżej zmieniając właśnie np. nazwę typu wtyczki. W przykładowym pliku ichnim - działa.