Export types

Nov 6, 2009 at 12:39 PM


Firstly, I have a base interface IPlugin with an associated string attribute "Plugin", now if I add a second string attribute "Type" and query the container I end up with 4 exports rather than the 2 exports that I have created (being PluginA and PluginB. I find this quite bizarre but i'm sure i'm missing the point somewhere here.

Secondly, I would like to create 2 further interfaces (IClient, IServer) both of which will implement IPlugin and get the associated Plugin Attributes... something like:


class PluginA : IClient, IPlugin

{ .... }

I should then be able to export this as IPlugin but use it within my app as IClient.

Hope this makes some sense, any help much appreciated.







Nov 6, 2009 at 3:38 PM

Seemed to have answered both my questions.

first off I have craeted a "PluginAttrubutes" class with more than one member (Name and Mode) which works just fine.

Secondly, I am able to get all parts as IPlugin but am able to cast the instances to the the other interface types that they are.




Nov 6, 2009 at 5:01 PM

In general if you are exporting the Plugin as IPlugin you should not rely on it also implementing IClient. While it is possible to do a test to see if it is of that type (using the is/as operators) and then us it as that type you should avoid it if at all possible. Of course it also depends on your scenario if you are doing a single ImportMany of (IPlugin) then filtering the list by IClient or IServer then it may make since to do the type test and cast. However if you are independently importing IPlugins and IClients/IServers then I would actually Export the plugin under IClient or IServer as well as IPlugin.

Nov 6, 2009 at 8:24 PM

Thanks, so I can export as multiple types? how would this then affect my imports for which i'm using GetExports() into a Lazy<IPlugin,IPluginMetadata>> ?

Nov 6, 2009 at 11:53 PM

Yes you can export a particular type with multiple types. As long as you aren't using CreationPolicy.NonShared then it shouldn't effect your GetExports() into a Lazy<IPlugin,IPluginMetadata>[] at all. Unless you have metadata that is conflicting, in otherwords if your Plugin has a "name" metadata key then your Client must either use the same "name" (it would be shared even if it is on another custom attribute applied to the same type) or not have a metadata key of the same name.

Nov 7, 2009 at 7:00 AM

Oh dear, I am indeed using CreationPolicy.NonShared, so it appear that I am doing this wrong. Should I try using a direct [Import] into an IEnumerable<> within my main class and Composing the parts in a different way?