wpf 异常:无法绑定到目标方法,因为其签名或安全透明度与委托类型的不兼容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37180354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:48:22  来源:igfitidea点击:

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

c#wpfdelegates

提问by Brett Steigried-StatFord

        Type objectType = Application.Current.MainWindow.GetType();
            EventInfo CtrlEventInfo = objectType.GetEvent(strEventName);
            if (CtrlEventInfo == null)
            {
                return;
            }
            Type TypeDelegate = CtrlEventInfo.EventHandlerType;
            MethodInfo methodInfo = typeof(MyPOS.Controls.ActiveAccounts.ViewCtrl).GetMethod(strMethodName, BindingFlags.NonPublic | BindingFlags.Instance);
            Delegate DelHandler = Delegate.CreateDelegate(TypeDelegate, this, methodInfo);
            CtrlEventInfo.AddEventHandler(Application.Current.MainWindow, DelHandler);

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

无法绑定到目标方法,因为其签名或安全透明度与委托类型的签名或安全透明度不兼容

is thrown in line

排成一排

Delegate DelHandler = Delegate.CreateDelegate(TypeDelegate, this, methodInfo);

采纳答案by Justin CI

I think you need to change your code to

我认为您需要将代码更改为

 MyPOS.Controls.ActiveAccounts.ViewCtrl obj= new MyPOS.Controls.ActiveAccounts.ViewCtrl();
 Delegate DelHandler = Delegate.CreateDelegate(TypeDelegate, obj, methodInfo);