.NET 7移植后使用反射出现强制转换错误的问题咨询
.NET Framework迁移到.NET 7时反射强制转换失败问题
我正在把项目从.NET Framework移植到.NET 7,碰到了反射实例化后强制转换出错的问题。奇怪的是,出问题的类明明实现了对应的接口,而且这段代码在.NET 4.x里完全能正常运行。
相关代码如下:
foreach (Assembly pluginAssembly in pluginAssemblies) { try { // Look for class(s) with our interface and construct them foreach (var type in pluginAssembly.GetTypes()) { Type iDesigner = type.GetInterface(typeof(IFireworksDesigner).FullName); if (iDesigner != null) { object instance = Activator.CreateInstance(type); // creates an object IFireworksDesigner designer = (IFireworksDesigner)instance; // throws an exception // do stuff } } } catch(Exception ex) { //Something really bad must have happened. MessageBox.Show("Fatal error reflecting plugins in assembly '" + pluginAssembly.FullName + "'.\r\n" + "The error message is:\r\n\r\n" + ex.Message); } }
补充测试情况:
- .NET Framework 4.8调用.NET Standard 2.0插件:运行正常
- .NET 7调用.NET Standard 2.0插件:转换失败
- .NET 7调用.NET 7插件:转换失败
内容的提问来源于stack exchange,提问作者Christopher Painter
相关产品推荐
相关产品推荐

