Re: modular application

From: Saso Zagoranski (saso.zagoranski_at_guest.arnes.si)
Date: 02/06/04


Date: Fri, 6 Feb 2004 22:19:45 +0100

Thanks Sami!
You really helped me a lot!

Just one more thing :)
How do you "cast" an object if you have the Type of the object stored in
some variable t?
So you can't use:
myGpsObject = (Gps) myObject;

but instead:
myGpsObject = ??? t.GetType() ???

Again,
thanks a lot for your help

"Sami Vaaraniemi" <samivawantsnospam@jippii.fi> wrote in message
news:c004o1$ocr$1@phys-news1.kolumbus.fi...
>
> "Saso Zagoranski" <saso.zagoranski@guest.arnes.si> wrote in message
> news:c000in$nks$1@planja.arnes.si...
> > That's the approach I was thinking about...
> > Just a few more questions...
> > Do your plugins inherit from IPlugin or do the implement it? I mean do
you
> > have this:
> > class MyPlugin : System.Object, IPlugin ? (or MyPlugin:
> > System.Windows.Forms, IPlugin or ...)
>
> My plugins inherit from IPlugin - which is the same as saying they
implement
> IPlugin. They also happen to be Forms, so they look like this (the second
> option above):
>
> class MyPlugin : System.Windows.Forms, IPlugin
>
> Sometimes the plugins implement a plugin-specific interface, e.g.,
>
> interface ISpecialPlugin : IPlugin {...}
>
> class MySpecialPlugin : System.Windows.Forms, ISpecialPlugin
>
> All these interfaces (IPlugin, ISpecialPlugin, IHost) are defined in an
> assembly which is referenced by the plugins and by the shell.
>
> >
> > In this case, isn't the Type of MyPlugin actually MyPlugin? To get the
> > interface
> > you would then have to use MyPlugin.GetType().GetInterface("IPlugin")
and
> to
> > call
> > the methods of this interface you would then use MethodInfo.Invoke(...).
> > And most importantly... You can't cast an object of type MyPlugin to
> > IPlugin, can you?
>
> Yes, the actual type of MyPlugin is MyPlugin. Due to the fact that
MyPlugin
> and all other plugins inherit from IPlugin, the shell can easily access
the
> IPlugin interface when it loads the plugin:
>
> Assembly asm = Assembly.LoadFrom(<url>); // this string comes from

> e.g., a config file
> IPlugin plugin = (IPlugin)asm.CreateInstance("MyPlugin"); // the
string
> "MyPlugin" string comes from e.g., a config file
>
> This way the shell can call the methods of IPlugin directly and there is
no
> need to invoke them through reflection. The shell only uses the interface,
> it never uses the actual type MyPlugin.
>
> >
> > Here's what I do so far:
> > I load all the types in an assembly. I go through all the types and
search
> > if those types implement
> > IModule. I create an instance of the object which implements the IModule
> > interface with:
> > object myObject = Activator.CreateInstance(typeofObject);
> > I then use the MethodInfo.Invoke and PropertyInfo.GetValue, to start the
> > OnConnect method of
> > the plugin (OnConnect is part of IModule) and to get the list of
Services
> > (also part of IPlugin) that the plugin provides (if any).
>
> This sounds fine to me except that you don't necessarily have to use
> MethodInfo.Invoke. Instead, simply cast your object to IModule:
>
> object myObject = Activator.CreateInstance(typeofObject);
> IModule module = (IModule)myObject;
>
> In order for this to work though, you need to define the interface IModule
> in a separate assembly and have your loader application reference that
> assembly.
>
> > I then add "myObject", it's services and it's type into an array at the
> > host. So when some plugin would call:
> > Host.FindPlugin("GPS");
> > The Host would find that the GpsPlugin provides the GPS service and it
> would
> > provide the
> > object reference and Type to the plugin which requires the question.
> > The main problem is:
> > How do I cast the GpsPlugin (which is stored at the host and received as
> > System.Object) to the type
> > of GpsPlugin (note that the host stored the Type information)?
>
> My earlier comments already answer this. Define the interface IGpsPlugin
in
> a shared assembly. Have the plugin reference that assembly. Then in the
> plugin you can simply write:
>
> IGpsPlugin gpsPlugin =
(IGpsPlugin)Host.FindPlugin(typeof(IGpsPlugin));
>
> You will not need to use reflection if you define the interfaces in a
> separate assembly, and have the host and the plugins reference this
> assembly. I know that some developers prefer the flexibility of reflection
> over having interfaces. My personal preference (in this case) is type
safety
> through interfaces.
>
> > I wouldn't want to hardcode the casting... Is this even possible?
> >
> > Thanks,
> > saso
>
> Sure,
> Sami
>
>
>



Relevant Pages

  • Re: modular application
    ... My plugins inherit from IPlugin - which is the same as saying they implement ... Sometimes the plugins implement a plugin-specific interface, e.g., ... isn't the Type of MyPlugin actually MyPlugin? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: reiser4 plugins
    ... "Plugins" is a bad word. ... We now have a standard disk format plugin, ... I see no problems with providing at least as consistent an EA interface ... Only, zip files aren't just compression, so ...
    (Linux-Kernel)
  • Re: Email clients
    ... presumably this isn't a bug in Mail after all. ... but filters not that good (why don't they just license dspam or some ... totally sufficient, and the interface to them is very, very good). ... plugins are basically hacks against a "private" ...
    (uk.comp.sys.mac)
  • Driver Model 2 Proposal - Linux Kernel Performance v Usability
    ... This is not an attempt to have a Microkernel, or any move away from GNU/OSS ... It is worth remembering that for every Kernel hacker there must ... Implement binary kernel 'plugin' interface ... even FS 'plugins' could be created although IPL would be ...
    (Linux-Kernel)
  • Re: Cross-appdomain communication
    ... reference to that assembly in order to get the interface type, ... that object to talk to the other appdomain? ... I have an app that searches for plugins at ... >> while I want to create a list of plugins, I don't want those assemblies to ...
    (microsoft.public.dotnet.framework.remoting)