|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Re: Plugins and Late BindingI 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 Show quoteHide quote "Jon Shemitz" <j**@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 > > > You could also just drop plugins in your plugin directory, get the public
class names and pick those that supports your interface... Patrice -- Show quoteHide quote"Alan Seunarayan" <so0naz@ntlw0rld.c0m> a écrit dans le message de news:ECT6e.20107$p71.676@newsfe3-gui.ntli.net... > 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" <j**@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 > > > > > > > >
ASCII lookup table
Datagrid resizing rows on sort - not wanted VB.NET Equiv of... AxWebBrowser1 and code behind redirecting Datagrid - shown vertically instead of horizontally? Easy one about web forms Assigning a Null to Datetime Variable or Object Timer Event Not Firing in SystemFileWatcher... MDIParent property [ANN] April 12, 2005, "Visual Basic 2005 Language Enhancements" chat |
|||||||||||||||||||||||