迁移至netstandard2.0遇CS0246错误:找不到ExportAttribute
解决netstandard2.0下ExportAttribute找不到的问题
问题原因
ExportAttribute、ImportingConstructor、ImportMany这些特性属于MEF(Managed Extensibility Framework)组件,在.NET Framework中是系统默认提供的,但迁移到.NET Standard 2.0后,需要手动引入对应的NuGet包才能使用。
修复步骤
安装对应NuGet包
安装System.ComponentModel.Composition包,这个包包含了.NET Standard 2.0版本的MEF相关特性。可以通过NuGet包管理器搜索安装,或者使用以下命令:- Package Manager Console:
Install-Package System.ComponentModel.Composition - .NET CLI:
dotnet add package System.ComponentModel.Composition
- Package Manager Console:
添加命名空间引用
在使用这些特性的代码文件顶部,确保添加正确的using指令:using System.ComponentModel.Composition;
完成以上操作后,ExportAttribute相关的编译错误即可解决。
内容的提问来源于stack exchange,提问作者Aleksandr Zolotov
相关产品推荐
相关产品推荐

