Plugins and Late Binding  
Author Message
ajf3_net





PostPosted: Thu Jan 27 14:51:30 CST 2005 Top

Dotnet >> Plugins and Late Binding Hi,
I have some code to load some plug-ins, but the code requires me to know
the name of the class to load (here: SamplePlugin, derived from IPlugin) :

(Here is some C# code, but I use VB.Net to code my program)
using System;
using System.Reflection;

public class Driver
{
static void Main()
{
Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
Type t = assembly.GetType ("SamplePlugin");
IPlugin plugin = (IPlugin) Activator.CreateInstance(t);
plugin.SayHello();
}
}

The problem is, I don't want to have to know the name of the class I want to
load... I want to load the new module and get an instance of the class that
derives IPlugin. Is there a way to do so?

In C++ for the same purpose, I had a win32 dll with an extern function that
returned an instance of the contained class, so I loaded the dll, called the
function and I was ready to proceed. Is there something similar I can do
with .NET class libraries?

thanks

DotNet176  
 
 
Jon





PostPosted: Thu Jan 27 14:51:30 CST 2005 Top

Dotnet >> Plugins and Late Binding ThunderMusic wrote:

> I have some code to load some plug-ins, but the code requires me to know
> the name of the class to load (here: SamplePlugin, derived from IPlugin) :

> Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
> Type t = assembly.GetType ("SamplePlugin");

> The problem is, I don't want to have to know the name of the class I want to
> load... I want to load the new module and get an instance of the class that
> derives IPlugin. Is there a way to do so?

foreach (Type Exported in assembly.GetExportedTypes())
if (Exported.IsClass && Exported.GetInterface("IPlugin", true) !=
null)
;



--

www.midnightbeach.com
 
 
Jeff





PostPosted: Thu Jan 27 17:03:20 CST 2005 Top

Dotnet >> Plugins and Late Binding Or you can put the plugins in a "plugins" folder

http://www.geocities.com/jeff_louie/OOP/oop13.htm

Regards,
Jeff
>The problem is, I don't want to have to know the name of the class I
want to
load.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
 
Alan





PostPosted: Tue Apr 12 12:31:41 CDT 2005 Top

Dotnet >> Plugins and Late Binding Hello,
I have 'played around' with using plugins and came across the same
issue. One way around is to create an attribute that can only be used one
and only in a '[assembly:xxxx]' level. The attribute constructor can take a
string for description and a string or a type for another. Then when you
load that assembly just look for your assembly-based attribute and you will
have an indication of the class that you can instantiate. Also, in the
construct of your attribute you could check that the type you pass it is
compatible with your IPlugin interface.

Hope this helps, If you have any problems then drop me a line!

Alan Seunarayan
"Jon Shemitz" <EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> ThunderMusic wrote:
>
>> I have some code to load some plug-ins, but the code requires me to
>> know
>> the name of the class to load (here: SamplePlugin, derived from IPlugin)
>> :
>
>> Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
>> Type t = assembly.GetType ("SamplePlugin");
>
>> The problem is, I don't want to have to know the name of the class I want
>> to
>> load... I want to load the new module and get an instance of the class
>> that
>> derives IPlugin. Is there a way to do so?
>
> foreach (Type Exported in assembly.GetExportedTypes())
> if (Exported.IsClass && Exported.GetInterface("IPlugin", true) !=
> null)
> ;
>
>
>
> --
>
> www.midnightbeach.com


 
 
Alan





PostPosted: Tue Apr 12 12:32:20 CDT 2005 Top

Dotnet >> Plugins and Late Binding Hello,
I have 'played around' with using plugins and came across the same
issue. One way around is to create an attribute that can only be used one
and only in a '[assembly:xxxx]' level. The attribute constructor can take a
string for description and a string or a type for another. Then when you
load that assembly just look for your assembly-based attribute and you will
have an indication of the class that you can instantiate. Also, in the
construct of your attribute you could check that the type you pass it is
compatible with your IPlugin interface.

Hope this helps, If you have any problems then drop me a line!

Alan Seunarayan

"Jon Shemitz" <EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> ThunderMusic wrote:
>
>> I have some code to load some plug-ins, but the code requires me to
>> know
>> the name of the class to load (here: SamplePlugin, derived from IPlugin)
>> :
>
>> Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
>> Type t = assembly.GetType ("SamplePlugin");
>
>> The problem is, I don't want to have to know the name of the class I want
>> to
>> load... I want to load the new module and get an instance of the class
>> that
>> derives IPlugin. Is there a way to do so?
>
> foreach (Type Exported in assembly.GetExportedTypes())
> if (Exported.IsClass && Exported.GetInterface("IPlugin", true) !=
> null)
> ;
>
>
>
> --
>
> www.midnightbeach.com
"ThunderMusic" <EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> Hi,
> I have some code to load some plug-ins, but the code requires me to
> know
> the name of the class to load (here: SamplePlugin, derived from IPlugin) :
>
> (Here is some C# code, but I use VB.Net to code my program)
> using System;
> using System.Reflection;
>
> public class Driver
> {
> static void Main()
> {
> Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
> Type t = assembly.GetType ("SamplePlugin");
> IPlugin plugin = (IPlugin) Activator.CreateInstance(t);
> plugin.SayHello();
> }
> }
>
> The problem is, I don't want to have to know the name of the class I want
> to
> load... I want to load the new module and get an instance of the class
> that
> derives IPlugin. Is there a way to do so?
>
> In C++ for the same purpose, I had a win32 dll with an extern function
> that
> returned an instance of the contained class, so I loaded the dll, called
> the
> function and I was ready to proceed. Is there something similar I can do
> with .NET class libraries?
>
> thanks
>
>
>


 
 
Patrice





PostPosted: Tue Apr 12 12:57:37 CDT 2005 Top

Dotnet >> Plugins and Late Binding You could also just drop plugins in your plugin directory, get the public
class names and pick those that supports your interface...

Patrice

--

"Alan Seunarayan" <EMail@HideDomain.com> a écrit dans le message de
news:ECT6e.20107$EMail@HideDomain.com...
> Hello,
> I have 'played around' with using plugins and came across the same
> issue. One way around is to create an attribute that can only be used one
> and only in a '[assembly:xxxx]' level. The attribute constructor can take
a
> string for description and a string or a type for another. Then when you
> load that assembly just look for your assembly-based attribute and you
will
> have an indication of the class that you can instantiate. Also, in the
> construct of your attribute you could check that the type you pass it is
> compatible with your IPlugin interface.
>
> Hope this helps, If you have any problems then drop me a line!
>
> Alan Seunarayan
>
> "Jon Shemitz" <EMail@HideDomain.com> wrote in message
> news:EMail@HideDomain.com...
> > ThunderMusic wrote:
> >
> >> I have some code to load some plug-ins, but the code requires me to
> >> know
> >> the name of the class to load (here: SamplePlugin, derived from
IPlugin)
> >> :
> >
> >> Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
> >> Type t = assembly.GetType ("SamplePlugin");
> >
> >> The problem is, I don't want to have to know the name of the class I
want
> >> to
> >> load... I want to load the new module and get an instance of the class
> >> that
> >> derives IPlugin. Is there a way to do so?
> >
> > foreach (Type Exported in assembly.GetExportedTypes())
> > if (Exported.IsClass && Exported.GetInterface("IPlugin", true) !=
> > null)
> > ;
> >
> >
> >
> > --
> >
> > www.midnightbeach.com
> "ThunderMusic" <EMail@HideDomain.com> wrote in message
> news:EMail@HideDomain.com...
> > Hi,
> > I have some code to load some plug-ins, but the code requires me to
> > know
> > the name of the class to load (here: SamplePlugin, derived from IPlugin)
:
> >
> > (Here is some C# code, but I use VB.Net to code my program)
> > using System;
> > using System.Reflection;
> >
> > public class Driver
> > {
> > static void Main()
> > {
> > Assembly assembly = Assembly.LoadFrom ("myplugin.dll");
> > Type t = assembly.GetType ("SamplePlugin");
> > IPlugin plugin = (IPlugin) Activator.CreateInstance(t);
> > plugin.SayHello();
> > }
> > }
> >
> > The problem is, I don't want to have to know the name of the class I
want
> > to
> > load... I want to load the new module and get an instance of the class
> > that
> > derives IPlugin. Is there a way to do so?
> >
> > In C++ for the same purpose, I had a win32 dll with an extern function
> > that
> > returned an instance of the contained class, so I loaded the dll, called
> > the
> > function and I was ready to proceed. Is there something similar I can
do
> > with .NET class libraries?
> >
> > thanks
> >
> >
> >
>
>