Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type [closed]
Asked Answered
L

0

16

Having done nothing more than install Visual Studio 2012, our existing application now crashes when attempting to create a delegate.

Why would we be getting this error when running our application (not running in debug...just running the .exe normally...not having re-compiled, or done anything other than install Visual Studio 2012)?

Does Visual Studio 2012 update .net 4.0 WindowsFormsIntegration in some way?

Any suggestions on how to get around this?

'The invocation of the constructor on type 'MyWindowsFormsHost' that matches the specified binding constraints threw an exception.'

With internal exception:

Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type

The offending class and line:

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;
    }
}

UPDATE: Discovered that the NotifyActiveateApp method no longer exists on WindowsFormsHost. What I don't understand is how installing .net 4.5 with visual studio 2012 has affected my existing 4.0 application.

UPDATE: In order to get around this, I've used reflection to check if the NotifyActivateApp method exists. (If it doesn't exist, then the app is running in the patched .net version...and I don't have to worry about the activation bug this child focus code was written to fix).

    MethodInfo methodInfo = (typeof(WindowsFormsHost)).GetMethod("NotifyActivateApp", BindingFlags.NonPublic | BindingFlags.Instance);
if (methodInfo != null)
{
     this.childGotFocus = Delegate.CreateDelegate(typeof(NotifyChildFocus), this, "NotifyActivateApp") as NotifyChildFocus;
}

Note to Microsoft: Thanks for fixing your bug...I just wish you would have rolled it out in a way that didn't break existing code.

Lythraceous answered 22/8, 2012 at 15:59 Comment(4)
Installing VS2012 probably upgraded the .NET runtime to v4.5, which changed something. No idea what changed though.Nitty
Is your NotifyActiveApp method non-public? Have you checked msdn.microsoft.com/en-us/library/12f294ye.aspx and subsequently msdn.microsoft.com/en-us/library/stfy7tfc.aspx? And possibly even this: msdn.microsoft.com/en-us/library/dd233103.aspx. Down the rabbit hole.Default
NotifyActiveApp is on the base class: WindowsFormsHost. I have looked at those actually. I have not tried changing security for reflection, but I will. I'm confused about why this would happen, since our application was compiled against .net v4.0.Lythraceous
As far as how installing .Net 4.5 affected your 4.0 application, take a look at this post by Rick Strahl.Cockboat

© 2022 - 2024 — McMap. All rights reserved.