当前位置:网站首页>[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
边栏推荐
猜你喜欢

新库上线 | CnOpenData信息传输、软件和信息技术服务业工商注册企业基本信息数据

学习笔记:栈的应用1_递归(重点)

The new library online | CnOpenDataA shares of the listed company basic information data
Flask 教程 第二章:模板

门外汉掌握数据分析处理技术的路线图

2020-8-18js练习

jmeter逻辑控制器使用

神经网络论文Enhancing deep neural networks via multiple kernel learning

The new database is online | CnOpenData information transmission, software and information technology service industry basic information data of industrial and commercial registered enterprises

矩阵相乘
随机推荐
新库上线 | CnOpenDataA股上市公司基本信息数据
GeoServer入门学习:06-发布多层级TIF地图大图数据
【时间戳转普通时间格式的方法】
简单Swing界面笔记
Flask 教程 第四章:数据库
Kotlin's JSON format parsing
Jmeter常见问题处理及常用功能
Kotlin笔记-ForEach与ForEachIndexed区别
单片机——串口通信(从串口接收多位数据保存到数组,发送多位数据到串口)
window下socket(udp)控制台程序
各类测试场景的检查点
charles简单使用
第十三届蓝桥杯(Web 应用开发)线上模拟赛【第九题】(知乎首页数据动态化)
C#实现Everything——数据显示
GeoServer入门学习:05-多层级MBTiles规范数据发布
矩阵相乘
Kotlin delegate property knowledge points
fastapi-实战-综述
第06篇 MEF部件的生命周期(PartCreationPolicy)
Bluu Seafood launches first lab-grown fish products