Board index » Visual Studio » Plugins and Late Binding

Plugins and Late Binding

Visual Studio70
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


-
 

Re:Plugins and Late Binding

ThunderMusic wrote:



Quote
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

-

Re: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" <jon@midnightbeach.com>wrote in message

Quote
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





-

Re: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" <jon@midnightbeach.com>wrote in message

Quote
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" <NOdanylat@sympatico.caSPAMATALL>wrote in message

Quote
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











-

Re: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" <so0naz@ntlw0rld.c0m>a écrit dans le message de

Quote
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" <jon@midnightbeach.com>wrote in message

news:41F95452.767F0885@midnightbeach.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" <NOdanylat@sympatico.caSPAMATALL>wrote in message

news:ueWuxEKBFHA.2676@TK2MSFTNGP12.phx.gbl...

>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

>

>

>









-