当前位置:网站首页>[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
边栏推荐
猜你喜欢
随机推荐
PHP传递任意数量的函数参数
Little knowledge about KotlinAndroid encounters
语义分割FCN FPN UNet DeepLab HRNet SETR TransFuse...
SQL-堆叠注入(含例题)
rust-异常机制
1天搞定单片机中断——基础知识大全
目标检测论文 Bridng the Gap Between Anchor-based and Anchor-free Detection via ATSS
使用fontforge修改字体,只保留数字
GeoServer入门学习:02-安装部署
Bagging、Boosting、Stacking集成学习代码
记录非Gui模式Jmeter使用
GeoServer入门学习:06-发布多层级TIF地图大图数据
用固态U盘让你的办公环境随身移动
学习笔记:2.3 静态链表 循环链表 双向链表
内网渗透之代理转发
go基于泛型实现继承
图神经网络GNN简介及应用方向
暑期“小候鸟”超员增多 惠州交警提醒:安全出行不能忘
【导出PDF-项目应用】
跨域问题 什么时候出现跨域问题 如何解决跨域问题