Eri*_*ang 16 c# delegates windowsformshost visual-studio-2012
除了安装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;
}
}
Run Code Online (Sandbox Code Playgroud)
更新:发现WindowsFormsHost上不再存在NotifyActiveateApp方法.我不明白的是,使用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;
}
Run Code Online (Sandbox Code Playgroud)
Microsoft的注意事项:感谢您修复您的错误...我希望您能够以不破坏现有代码的方式推出它.