You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 16:50:29