I don't know if this will help you 100%, but with this code im controls the loading of my modules.
If you can control your download order, you can put all of your * .dll in the same folder and save some time by finding them in subfolders:
The key to this is to use this additional attribute: [ExportMetadata("Order", 1)]
Then your plugin should look like this:
[Export(typeof(YourContract))] [ExportMetadata("Order", 1)] public class YourPlugin: YourContract{}
To get things loaded in the correct order, you will need something like this:
Interface:
public interface IOrderMetadata { [DefaultValue(int.MaxValue)] int Order { get; } }
AdaptingCollection:
public class AdaptingCollection<T, M> : ICollection<Lazy<T, M>>, INotifyCollectionChanged { /// <summary> /// Constructor</summary> public AdaptingCollection() : this(null) { } /// <summary> /// Constructor</summary> /// <param name="adaptor">Function to apply to items in the collection</param> public AdaptingCollection(Func<IEnumerable<Lazy<T, M>>, IEnumerable<Lazy<T, M>>> adaptor) { this._mAdaptor = adaptor; } /// <summary> /// CollectionChanged event for INotifyCollectionChanged</summary> public event NotifyCollectionChangedEventHandler CollectionChanged; /// <summary> /// Force the adaptor function to be run again</summary> public void ReapplyAdaptor() { if (this._mAdaptedItems == null) return; this._mAdaptedItems = null; this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); }
Oderingcollection
public class OrderingCollection<T, M> : AdaptingCollection<T, M> { /// <summary> /// Constructor</summary> /// <param name="keySelector">Key selector function</param> /// <param name="descending">True to sort in descending order</param> public OrderingCollection(Func<Lazy<T, M>, object> keySelector, bool descending = false) : base(e => descending ? e.OrderByDescending(keySelector) : e.OrderBy(keySelector)) { } }
Using
[ImportMany(typeof(YourContract), AllowRecomposition = true)] internal OrderingCollection<YourContract, IOrderMetadata> Plugins{ get; private set; }
In your constructor:
this.Plugins= new OrderingCollection<ITemplateMapper, IOrderMetadata>( lazyRule => lazyRule.Metadata.Order);
My boot code (may be different from yours):
private void LoadModules() { var aggregateCatalog = new AggregateCatalog(); aggregateCatalog.Catalogs.Add(new DirectoryCatalog(".", "*.Plugin.*.dll")); var container = new CompositionContainer(aggregateCatalog); container.ComposeParts(this); }
Hope this helps you get rid of MAF