[英]Unknown Exception when using .NET 4.5 and Mixed Assemblies
I have a problem when the new .Net Framework (version 4.5) is installed. 安装新的.Net Framework(版本4.5)时出现问题。 I am working on a plugin assembly that is accessed through native code.
我正在研究通过本机代码访问的插件程序集。 The library is build using Visual Studio 2010 and with .NET 4.0 as target plattform.
该库是使用Visual Studio 2010并以.NET 4.0作为目标平台构建的。
The following code snippet throws an exception in .NET 4.5 and is working fine in .NET 4.0. 以下代码段在.NET 4.5中引发异常,并在.NET 4.0中正常运行。 Any idea how to fix this issue?
任何想法如何解决此问题?
#pragma managed(push, off)
#pragma managed(push, on)
static bool ManagedTestFunction() {
return false;
}
#pragma managed(pop)
static void CheckIfManagedUnmanagedTransitionIsPossible()
{
try
{
ManagedTestFunction();
}
catch (...)
{
throw misc::mwException(0, _T("The program is not working if .NET Framework 4.5 is installed. Please revert to previous version .NET 4.0!"));
}
};
Best regards 最好的祝福
Michael 麦可
我将看看最初引发了什么异常-应该为您提供一个线索,说明为什么代码无法在.NET Framework 4.5下工作。
finally I solve the problem myself. 最后我自己解决了这个问题。 It is maybe a conceptional problem due to some optimization in .NET 4.5.
由于.NET 4.5中的一些优化,这可能是一个概念性问题。 Concider the following example:
考虑以下示例:
// managed class
ref class A
{
}
// native class holding a reference to A
class B
{
msclr::gcroot<A^> m_refA;
};
When first crossing the native-managed boundary, the new .NET Framework tries to resolve all managed types that are reference in native classes independent whether they are actually used or not. 当首次越过本机托管的边界时,新的.NET Framework将尝试解析本机类中引用的所有托管类型,而与它们是否实际使用无关。 At that time, it is not yet possible to subscribe to the AppDomain.ResolveAssembly event.
那时,尚无法预订AppDomain.ResolveAssembly事件。 If the framework cannot resolve a type, the crash happens.
如果框架无法解析类型,则会发生崩溃。
A possible solution is modify class B such that it stores a reference to a System::Object^ instead and to cast dynamically using "cli_safe((System::Object^) m_refA)" 一种可能的解决方案是修改类B,以便它存储对System :: Object ^的引用,并使用“ cli_safe((System :: Object ^)m_refA)”进行动态转换。
Best regards 最好的祝福
Michael 麦可
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.