开发者

Getting multiple MEF catalog/container

开发者 https://www.devze.com 2023-03-25 12:09 出处:网络
As is answered in this post, I can use this code for getting plugins using catalog/container. var catalog = new AggregateCatalog();

As is answered in this post, I can use this code for getting plugins using catalog/container.

var catalog = new AggregateCatalog(); 
catalog.Catalogs.Add(new DirectoryCatalog(@"./")); // or different directory
var container = new CompositionContainer(catalog);
container.ComposeParts(this);

How is this code modified when I need multiple plugins? For example, if I have two plugins : one for getting color using interface Icolor, and the other for size using interface Isize, how can I discriminate between the two?

I can think of having two different directories for both color and size, but I don't think this is a general solution.

// get catalog and container
AggregateCatalog catalogForColor = new AggregateCatalog();
catalogForColor.Catalogs.Add(new DirectoryCatalog(@"/pluginForColorDirectory"));
var containerForColor = new CompositionContainer(catalogForColor);
containerForColor.ComposeParts(this);

AggregateCatalog catalogForSize = new AggregateCatalog();
catalogForSize.Catalogs.Add(new DirectoryCatalog(@"/pluginForSizeDirectory"));
var catalogForSize = new CompositionContainer(catalogForSize);
catalogForSize.ComposeParts(this);

// the property to store the catalog 
public IEnumerable<Lazy<Icolor, IMessa开发者_运维知识库geSenderMetadata>> Color { get; set; }
public IEnumerable<Lazy<Isize, IMessageSenderMetadata>> Size { get; set; }


DrectoryCatalog and AssemblyCatalog instances will scan assemblies for ALL exported parts. This will include both types of your plugin. You should't have to create a whole new catalog and container for composing these types, it should just work as-is.

0

精彩评论

暂无评论...
验证码 换一张
取 消