I have an application that accepts plugins. It allows the user to choose
which plugins they want to run from a list of available plugins. My problem
is that I need to build that list of plugins by checking the assemblies in
the plugins folder to see if they implement the plugin interface. If I load
each assembly and check it, then I have all those assemblies in memory until
the application gets shutdown, even though the user may only want to use a
few of them.

My idea was to load them all into a separate appdomain to check them and
build my list, and then unload that domain. Then I could just load the
plugins that the user chooses. My problem is that I don't know how to check
the assembly to see if it implements the plugin interface once its loaded
into the separate appdomain. Can anyone point me to an article on this, or
some sample code? Thanks!

Re: Checking for implementation of an interface by Lloyd

Lloyd
Thu Jun 02 23:12:40 CDT 2005

you should be able to use remoting...

"Bagger" <Bagger@discussions.microsoft.com> wrote in message
news:75536FBD-132F-4BA1-9C17-AFB887FF5D6C@microsoft.com...
>I have an application that accepts plugins. It allows the user to choose
> which plugins they want to run from a list of available plugins. My
> problem
> is that I need to build that list of plugins by checking the assemblies in
> the plugins folder to see if they implement the plugin interface. If I
> load
> each assembly and check it, then I have all those assemblies in memory
> until
> the application gets shutdown, even though the user may only want to use a
> few of them.
>
> My idea was to load them all into a separate appdomain to check them and
> build my list, and then unload that domain. Then I could just load the
> plugins that the user chooses. My problem is that I don't know how to
> check
> the assembly to see if it implements the plugin interface once its loaded
> into the separate appdomain. Can anyone point me to an article on this,
> or
> some sample code? Thanks!



Re: Checking for implementation of an interface by Jon

Jon
Fri Jun 03 00:54:00 CDT 2005

Bagger <Bagger@discussions.microsoft.com> wrote:
> I have an application that accepts plugins. It allows the user to choose
> which plugins they want to run from a list of available plugins. My problem
> is that I need to build that list of plugins by checking the assemblies in
> the plugins folder to see if they implement the plugin interface. If I load
> each assembly and check it, then I have all those assemblies in memory until
> the application gets shutdown, even though the user may only want to use a
> few of them.
>
> My idea was to load them all into a separate appdomain to check them and
> build my list, and then unload that domain. Then I could just load the
> plugins that the user chooses. My problem is that I don't know how to check
> the assembly to see if it implements the plugin interface once its loaded
> into the separate appdomain. Can anyone point me to an article on this, or
> some sample code? Thanks!

I suspect the easiest way to find some sample code is to download the
NUnit sources - NUnit *definitely* loads the test assemblies into a
different AppDomain, and then uses them.

I suspect you'll need to load one of your own assemblies into the new
AppDomain as well, and ask that to do the searching for you. That way
you don't need to worry nearly so much about marshalling :)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Checking for implementation of an interface by Bagger

Bagger
Fri Jun 03 02:37:37 CDT 2005



"Jon Skeet [C# MVP]" wrote:

> Bagger <Bagger@discussions.microsoft.com> wrote:
> > I have an application that accepts plugins. It allows the user to choose
> > which plugins they want to run from a list of available plugins. My problem
> > is that I need to build that list of plugins by checking the assemblies in
> > the plugins folder to see if they implement the plugin interface. If I load
> > each assembly and check it, then I have all those assemblies in memory until
> > the application gets shutdown, even though the user may only want to use a
> > few of them.
> >
> > My idea was to load them all into a separate appdomain to check them and
> > build my list, and then unload that domain. Then I could just load the
> > plugins that the user chooses. My problem is that I don't know how to check
> > the assembly to see if it implements the plugin interface once its loaded
> > into the separate appdomain. Can anyone point me to an article on this, or
> > some sample code? Thanks!
>
> I suspect the easiest way to find some sample code is to download the
> NUnit sources - NUnit *definitely* loads the test assemblies into a
> different AppDomain, and then uses them.
>
> I suspect you'll need to load one of your own assemblies into the new
> AppDomain as well, and ask that to do the searching for you. That way
> you don't need to worry nearly so much about marshalling :)
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

Thanks for the help :) I've been hunting around for info on this, and I've
read in a couple places that I would need to use a proxy. Is that what you
were referring to when you said I'd need to load one of my own assemblies
into the new appdomain? This is my first foray into this kind of stuff, so
I'm trying to learn as quick as possible :)

Re: Checking for implementation of an interface by mdb

mdb
Fri Jun 03 11:22:03 CDT 2005

=?Utf-8?B?QmFnZ2Vy?= <Bagger@discussions.microsoft.com> wrote in
news:75536FBD-132F-4BA1-9C17-AFB887FF5D6C@microsoft.com:

> I have an application that accepts plugins. It allows the user to
> choose which plugins they want to run from a list of available
> plugins. My problem is that I need to build that list of plugins by
> checking the assemblies in the plugins folder to see if they implement
> the plugin interface. If I load each assembly and check it, then I
> have all those assemblies in memory until the application gets
> shutdown, even though the user may only want to use a few of them.

You can prevent that by loading the assembly's raw byte[] data instead of
using Assembly.LoadFrom()... in other words,

byte[] aData;
FileStream fs = File.OpenRead(...);
aData = new byte[fs.Length];
fs.Read(aData, 0, aData.Length);
fs.Close();
Assembly a = Assembly.Load(aData);

(this is from memory so it might not exactly correct.)

> My idea was to load them all into a separate appdomain to check them
> and build my list, and then unload that domain. Then I could just
> load the plugins that the user chooses. My problem is that I don't
> know how to check the assembly to see if it implements the plugin
> interface once its loaded into the separate appdomain. Can anyone
> point me to an article on this, or some sample code? Thanks!

I'm not sure if this is the most efficient way to do it, but it works...
Load your assembly into 'Assembly a'...

Assembly a;
Type[] t = a.GetTypes();
for(int i=0; i<t.Length; i++)
{
object o = null;
IPlugin PluginObj = null;
Type[] interfaces = t[i].GetInterfaces();
for(int j=0; j<interfaces.Length; j++)
{
if (interfaces[j] == typeof(IPlugin))
{
try
{
o = a.CreateInstance(t[i].FullName);
PluginObj = (IPlugin)o;
...

Also, if you load the Assembly as in your first question and you want to
use it as a plugin... I'm not sure how GarbageCollection would handle
things after you're finished with Assembly a, so you might need to either
(a) Keep a reference to the 'Assembly a' variable, or (b) just reload the
assembly again with LoadFrom(...).

--
-mdb