1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
using System;
using System.Collections.Generic;
using System.Linq;
//ajout ref au projet lib Contracts
using Contracts;
//ajout ref à assembly System.ComponentModel.Composition(c'est le MEF)
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.IO;
namespace ImportingLib
{
//contain the host, hosts and calls the components
public class Importer
{
[ImportMany(typeof(IMyComponent))]
private IEnumerable<IMyComponent> operations;
public void DoImport()
{
//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
//Adds all the parts found in all assemblies in
//the same directory as the executing program
catalog.Catalogs.Add(
new DirectoryCatalog(
Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location)));
//Create the CompositionContainer with the parts in the catalog
CompositionContainer container = new CompositionContainer(catalog);
//Fill the imports of this object
container.ComposeParts(this);
}
public int AvailableNumberOfOperations
{
get
{
return (operations != null ? operations.Count() : 0);
}
}
public List<string> CallAllComponents( string a, string b)
{
var result = new List<string>();
foreach( var op in operations )
{
Console.WriteLine(op.Description);
result.Add( op.ManipulateString(a,b ));
}
return result;
}
}
} |
Partager