MethodInfo.CreateDelegate抛出异常

时间:2015-07-01 10:03:54

标签: .net vb.net reflection delegates

我在win8.1手机项目中遇到MethodInfo.CreateDelegate问题,即使我可以手动创建委托实例,它似乎也会返回错误。

Private thisworksFine As New returnDel(AddressOf WorkWith_Return)

我收到的错误是

  

methodIf.CreateDelegate(GetType(returnDel2)){System.ArgumentException:无法绑定到目标方法,因为其签名或安全透明度与委托类型的签名或安全透明度不兼容。在System.Reflection.RuntimeMethodInfo.CreateDelegateInternal(类型delegateType,Object firstArgument,DelegateBindingFlags bindingFlags,StackCrawlMark& stackMark)处于System.Reflection.RuntimeMethodInfo.CreateDelegate(Type delegateType)} System.ArgumentException

这是代码

Private Delegate Sub returnDel(intErrorId As Integer, strErrorMsg As String)

Private Sub WorkWith_Return(intErrorId As Integer, strErrorMsg As String)

End Sub

Sub sDeligateTesting()
    Dim methodInfos As MethodInfo() = GetType(MyClass).GetRuntimeMethods()

    For Each methodIf As MethodInfo In methodInfos
        If methodIf.Name = "WorkWith_Return" Then
            Dim ThisDoesNotWork = methodIf.CreateDelegate(GetType(returnDel)) <--Errror here
        End If
    Next
End Sub

任何人都可以通过指出我哪里出错来帮助我吗?

1 个答案:

答案 0 :(得分:1)

今天早上刚刚解决了,所以我想我会分享修复,以防其他人遇到同样的问题。我只需要添加me,代理与使用此行创建的代理匹配:

Private thisworksFine As New returnDel(AddressOf WorkWith_Return)

工作代码:

Dim ThisDoesNotWork = methodIf.CreateDelegate(GetType(returnDel),me)