除了安装 Visual Studio 2012 外,我们现有的应用程序现在在尝试创建委托时崩溃。
为什么在运行我们的应用程序时会出现此错误(未在调试中运行...只是正常运行 .exe...没有重新编译,或者除了安装 Visual Studio 2012 之外的任何其他操作)?
Visual Studio 2012 是否以某种方式更新 .net 4.0 WindowsFormsIntegration?
关于如何解决这个问题的任何建议?
'对匹配指定绑定约束的'MyWindowsFormsHost'类型的构造函数的调用引发了异常。
内部例外:
无法绑定到目标方法,因为它的签名或安全透明度与委托类型的不兼容
违规的班级和线路:
internal class MyWindowsFormsHost : WindowsFormsHost
{
private delegate void NotifyChildFocus(ref Message m);
private readonly NotifyChildFocus childGotFocus;
public MyWindowsFormsHost()
{
//this line crashes now (and did not before VS2012 install)
this.childGotFocus = Delegate.CreateDelegate(typeof(NotifyChildFocus),
this, "NotifyActivateApp") as NotifyChildFocus;
}
}
更新:发现 NotifyActiveApp 方法不再存在于 WindowsFormsHost 上。我不明白的是使用 Visual Studio 2012 安装 .net 4.5 如何影响我现有的 4.0 应用程序。
更新:为了解决这个问题,我使用反射来检查 NotifyActivateApp 方法是否存在。(如果它不存在,那么该应用程序正在修补的 .net 版本中运行……我不必担心编写此子焦点代码来修复的激活错误)。
MethodInfo methodInfo = (typeof(WindowsFormsHost)).GetMethod("NotifyActivateApp", BindingFlags.NonPublic | BindingFlags.Instance);
if (methodInfo != null)
{
this.childGotFocus = Delegate.CreateDelegate(typeof(NotifyChildFocus), this, "NotifyActivateApp") as NotifyChildFocus;
}
微软注意:感谢您修复您的错误...我只是希望您能够以一种不会破坏现有代码的方式推出它。