1 Star 0 Fork 0

demo / MefDemo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Program.cs 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
liuxiaopei 提交于 2020-05-16 19:25 . add:first commit
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace MefDemo
{
[Export]
public class Program
{
private static CompositionContainer container;
[ImportMany]
IEnumerable<IBook> bookList { get; set; }
[Import]
MyBooks myBook;
static void Main(string[] args)
{
LoadApp();
//
Console.WriteLine("-------------------------");
Program program = container.GetExportedValue<Program>();
if (program != null && program.bookList != null)
{
foreach(IBook book in program.bookList)
{
book.SayHi();
}
}
//
Console.WriteLine("-------------------------");
IBook boo= container.GetExportedValue<IBook>("math");
boo.SayHi();
//
Console.WriteLine("-------------------------");
if (program.myBook != null)
{
program.myBook.SayHi();
}
Console.ReadLine();
}
static void LoadApp()
{
AggregateCatalog catalog = new AggregateCatalog();
container = new CompositionContainer(catalog);
catalog.Catalogs.Add(new DirectoryCatalog(Directory.GetCurrentDirectory()));
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
}
}
}
C#
1
https://gitee.com/liuxp_demo/MefDemo.git
git@gitee.com:liuxp_demo/MefDemo.git
liuxp_demo
MefDemo
MefDemo
master

搜索帮助