|
-
Apr 16th, 2006, 03:30 AM
#1
[RESOLVED] [2.0] Issues setting up a Plugin system
I've gone through many plugin examples as well as the one in the VB.Net codebank and I just cannot seem to get it working correctly.
1. After I create an interface, let's call it IPlugin, and I create a DLL that uses that interface, what's the easyest and/or best way to test DLLs to see if it contains that interface?
2. How do I call a function of a DLL that supports the IPlugin interface I created (let's say it only has 1 method, GetMyName() or something like that)? Remember, this is all handled dynamically (no adding references via the IDE).
If someone can point me to a good tutorial I will happily go through it but if someone could post some sample code to do 1 and 2 that would be extremely helpful!
-
Apr 16th, 2006, 05:50 AM
#2
Re: [2.0] Issues setting up a Plugin system
You could use reflection to get the types that your library contains. You can then create an instance and use reflection again to get the interfaces implemented by that object. If it implements the IPlugin interface then you can cast it as an IPlugin instance and then access its members. There may well be a better way than that but I've never tried to do this so that's off the top of my head.
-
Apr 16th, 2006, 06:17 AM
#3
Re: [2.0] Issues setting up a Plugin system
Hmm... I'll see what I can dig up on the MSDN and try to work through this myself some more. If anyone has any code examples I'd greatly appreciate it.
-
Apr 16th, 2006, 02:29 PM
#4
Re: [2.0] Issues setting up a Plugin system
Here's an example from one of my projects, a chat client.
Code:
// the IHost interface provides events that the
// plugin can subscribe to to interact with the main program,
// as well as a function which returns the main 'ChatBot' object.
public interface IPlugin
{
bool Initialize(IHost Host);
bool Shutdown();
void ShowAbout();
string Author();
string Name();
string Version();
string Info();
}
And in the main program,
Code:
static void LoadPlugins(Settings settings)
{
// settings.Plugins is a string[]
foreach (string sPlugin in settings.Plugins)
{
try
{
Assembly a = Assembly.LoadFile(System.IO.Path.Combine(Program.Path(), sPlugin));
Type[] types = a.GetTypes();
foreach (Type t in types)
{
if (t.GetInterface("IPlugin", true) != null)
{
IPlugin plugin = (IPlugin)Activator.CreateInstance(t);
if (plugin.Initialize(host))
plugins.Add(plugin);
}
}
}
catch
{
// host.WriteLine("Failed to load plugin: " + sPlugin)
}
}
}
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Apr 16th, 2006, 08:13 PM
#5
Re: [2.0] Issues setting up a Plugin system
Thanks a lot! I am having an issue though with this part:
VB Code:
IPlugin plugin = (IPlugin)Activator.CreateInstance(t);
My DLL is going to consist of a Custom UserControl that also inherits the IPlugin interface (so it's declared like the following)
VB Code:
public class MyCom : UserControl, IPlugin
{
}
So the base of this plugin will be both a UserControl and an IPlugin. How can I cast this correctly so I can access it? Or do you have any other recommendations?
-
Apr 16th, 2006, 08:24 PM
#6
Re: [2.0] Issues setting up a Plugin system
IPlugin is not the base for your class. .NET does not allow multiple inheritance. UserControl is the immediate base class and your class implements the IPlugin interface. They are not the same thing. You can cast an variable to any type that it inherits either directly or indirectly, or to any interface that it implements. That means that if you have a variable of type MyCom then you can cast it as UserControl, IPlugin, Object, or any other type between Object and UserControl, including Control, Component, etc. Whatever type you cast it as is the type whose members you can access. If you cast as IPlugin then you cannot access members of the UserControl class, only the IPlugin interface.
-
Apr 16th, 2006, 08:34 PM
#7
Re: [2.0] Issues setting up a Plugin system
But it throws an exception stating "Invalid Cast Exception from MyCom to IPlugin"
-
Apr 16th, 2006, 08:48 PM
#8
Re: [2.0] Issues setting up a Plugin system
Are you sure that it is the same IPlugin interface that the application and the library are referring to? You haven't declared two interfaces with the same name, one in the application and one in the library, have you? As I say, I've never done this myself but I would assume that the IPlugin interface would have to be declared in a library spearate to both the application and the plugin, so that both could reference it and be referring to the same type.
-
Apr 16th, 2006, 09:07 PM
#9
Re: [2.0] Issues setting up a Plugin system
 Originally Posted by jmcilhinney
Are you sure that it is the same IPlugin interface that the application and the library are referring to? You haven't declared two interfaces with the same name, one in the application and one in the library, have you? As I say, I've never done this myself but I would assume that the IPlugin interface would have to be declared in a library spearate to both the application and the plugin, so that both could reference it and be referring to the same type.
Ah, thanks! Works perfectly now!
I didn't know I couldn't declare the two interfaces in seperate libraries. I assume since they were in the same namespace and had the same names it would be nice. Whoops
-
Apr 17th, 2006, 12:18 AM
#10
Re: [RESOLVED] [2.0] Issues setting up a Plugin system
I do have 1 more question about this topic. When I use the same technique as sunburnt posted. I need to constantly check data on my plugins (sometimes it may be a lot in a few seconds, sometimes it may be none for an hour). If I create a List<IPlugin> and load all of the interfaces into the list, how expensive will that be? Will it include the whole UserControl? Or should I use a different method (like an array of strings that have the Plugin's path and just seem reloading it (though this may not fit with my design))?
-
Apr 17th, 2006, 01:40 AM
#11
Re: [RESOLVED] [2.0] Issues setting up a Plugin system
A collection merely contains references to objects. If the objects already exist then creating a collection to store references to them is very little overhead at all. It's just basically an instance of a class that has an internal array, the elements of which are references to your IPlugin objects. No additional IPlugin objects are created.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|