当前位置:网站首页>[MEF] Chapter 04 MEF's Multi-Component Import (ImportMany) and Directory Services
[MEF] Chapter 04 MEF's Multi-Component Import (ImportMany) and Directory Services
2022-08-08 21:01:00 【thinking barefoot】
I. Demo overview
This demo shows how MEF uses the ImportMany feature to import multiple export parts that match the same constraints at the same time, and introduces the Catalog service, which tells the MEF framework where to search for components that match the specified constraintsThe export part, that is, where the export part is located.
Related downloads (screen recordings, code):http://yunpan.cn/cVdN5JHeQrJgI Access code 065d
Warm reminder: If the screen recording and code cannot be downloaded normally, you can leave a message on the site, or send an email to [email protected]
Welcome netizens who are interested in researching .NET related technologies to join the QQ group: 18507443
2. ImportMany of multiple parts
The export settings of the component do not change, and the import place is modified like the following:
[ImportMany(typeof(ILogger))]public IEnumerable Loggers { get; set; }
There are two differences between the above code and the import of a single component:1. Use the ImportMany feature instead of the Import feature.
2. The IEnumerable
After the import element is modified as above, it only needs to traverse to access each export component when calling.As shown in the following code:
static void Main(string[] args){Program program = new Program();program.Compose();foreach (ILogger logger in program.Loggers){logger.WriteLog("Hello World!");}Console.ReadLine();}
III. Catalog
In MEF, the so-called directory (Catalog) is different from the directory (Directory) in the file system, and its role is to tell the MEF framework where to find the exported components, such as:
AssemblyCatalog: Finds the exported assembly in the specified assembly.
DirectoryCatalog: Find the export component in the specified file directory, the file directory can be passed in the constructor, which can be an absolute path or a relative path.
The code is as follows:
/// /// Assembles the host and widget together via the container object./// public void Compose(){AggregateCatalog aggregateCatalog = new AggregateCatalog();AssemblyCatalog assemblyCatalog = new AssemblyCatalog(typeof(Program).Assembly);DirectoryCatalog directoryCatalog = new DirectoryCatalog("imps");aggregateCatalog.Catalogs.Add(assemblyCatalog);aggregateCatalog.Catalogs.Add(directoryCatalog);var container = new CompositionContainer(aggregateCatalog);container.ComposeParts(this);}
There is a catalog class of AggregateCatalog in the above code, which is an aggregate catalog, which can organize multiple catalogs together, such as AssemblyCatalog and DirectoryCatalog in the above column.In this way, MEF will search for a matching exported part in multiple places.IV. Relevant resources
1. MSDN official information:http://msdn.microsoft.com/en-us/library/dd460648(v=vs.110).aspx
边栏推荐
猜你喜欢
随机推荐
新规划|广州都市圈将以广佛为核心,广佛将有18条地铁相连通
使用LBP特征进行图像分类
文档图像二值化DIB_paper_1
window下socket(udp)控制台程序
Gradle简单到使用kotlin编写到常用命令
并发和并行——从线程,线程池到任务
学习笔记:2.3 静态链表 循环链表 双向链表
第十三届蓝桥杯(Web 应用开发)线上模拟赛【第九题】(知乎首页数据动态化)
IO in Kotlin flow
Kotlin中IO流
单片机——DHT11 温湿度传感器
暑期“小候鸟”超员增多 惠州交警提醒:安全出行不能忘
第06篇 MEF部件的生命周期(PartCreationPolicy)
Flask 教程 第十一章:美化
常见的病毒(攻击)分类
Kotlin's JSON format parsing
Mysql管理指令
昇腾Ascend 随记 —— TensorFlow 模型迁移
Flask 教程 第七章:错误处理
学习笔记:栈的应用1_递归(重点)