Plugins and Late Binding

Discussion in 'ASP .Net' started by ThunderMusic, Jan 27, 2005.

  1. ThunderMusic

    ThunderMusic Guest

    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
    ThunderMusic, Jan 27, 2005
    #1
    1. Advertising

  2. ThunderMusic

    Jon Shemitz Guest

    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
    Jon Shemitz, Jan 27, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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 Seunarayan, Apr 12, 2005
    #3
  4. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    news:...
    > 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
    >
    >
    >
    Alan Seunarayan, Apr 12, 2005
    #4
  5. ThunderMusic

    Patrice Guest

    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" <0m> a écrit dans le message de
    news:ECT6e.20107$...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > news:...
    > > 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, Apr 12, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PatLaf

    problem with owc10 and late binding

    PatLaf, Jan 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    321
    Ken Cox [Microsoft MVP]
    Jan 10, 2004
  2. Dennis Chang
    Replies:
    3
    Views:
    385
    Dennis Chang
    May 15, 2004
  3. Olaf Meding

    MS COM early and late binding

    Olaf Meding, Apr 27, 2004, in forum: Python
    Replies:
    4
    Views:
    393
    Simon Brunning
    Apr 28, 2004
  4. Replies:
    2
    Views:
    846
    Kevin Grover
    Oct 20, 2006
  5. Replies:
    0
    Views:
    292
Loading...

Share This Page