C#使用MEF进行模块化应用程序开发

MEF介绍

MEF全称Managed Extensibility Framework ,是一个用于创建可扩展的轻量级应用程序的库。 MEF 让扩展不仅可在应用程序内重复使用,还可以跨程序重复使用。

MEF随.Net Framework 4一起发布,默认命名空间是System.ComponentModel.Composition。称为MEF1版本,在下面的文章中,MEF默认指MEF1版本

如果需要在.Net Core中使用MEF,可以使用Nuget包管理器安装

1 Install-Package System.ComponentModel.Composition -Version 5.0.0-preview.6.20305.6

WPF/Winform中使用时,直接引用GAC中的System.ComponentModel.Composition即可

如果需要在Web应用程序中使用MEF

可以使用MEF2版本(支持.Net Core),默认命名空间是System.Composition

1 Install-Package System.Composition -Version 5.0.0-preview.6.20305.6

可通过以下链接,了解MEF1和MEF2的区别

https://github.com/dotnet/runtime/blob/master/src/libraries/System.ComponentModel.Composition/mef_guide/README.md

MEF2的使用方法可参见

C#高级编程(第10版) 第26章 Composition 754

附加内容:

Visual Studio的扩展功能使用了MEF框架来进行实现,可访问以下链接了解更多信息

https://docs.microsoft.com/en-us/visualstudio/extensibility/managed-extensibility-framework-in-the-editor?view=vs-2019

MEF使用

MEF主要是通过导入、导出部件来实现应用程序的可扩展性。

原文地址:https://www.cnblogs.com/zhaotianff/p/13277853.html