namespace BLL
{
[Export(typeof(ISortingInfo))]
public class Class1BLL : ISortingInfo
{
public string Init()
{
return "Class1";
}
}
}
namespace BLL
{
[Export(typeof(ISortingInfo))]
public class Class2BLL : ISortingInfo
{
public string Init()
{
return "Class__b";
}
}
}
3 主函数
namespace MEFTest
{
public class Program
{
//[Import(typeof(ISortingInfo))]
[ImportMany(typeof(ISortingInfo))]
public IEnumerable<ISortingInfo> iSortingInfo { get; set; }
static void Main(string[] args)
{
Console.WriteLine("7777777777777777777777777777777");
Program pro = new Program();
foreach (ISortingInfo item in pro.iSortingInfo)
{
Console.WriteLine(item.Init());
}
Console.Read();
}
private CompositionContainer _container;
public Program()
{
try
{
// An aggregate catalog that combines multiple catalogs.
//var catalog = new DirectoryCatalog(Environment.CurrentDirectory);
var catalog=new AggregateCatalog();
// Adds all the parts found in the same assembly as the Program class.
//catalog.Catalogs.Add(new AssemblyCatalog(typeof(Class1BLL).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.LoadFrom(Environment.CurrentDirectory+"\\BLL.dll")));
// Create the CompositionContainer with the parts in the catalog.
_container = new CompositionContainer(catalog);
_container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
}
}