Trev
Trev

Reputation: 21

MethodInfo.CreateDelegate throws exception

I am having problems with the MethodInfo.CreateDelegate on a win8.1 phone project, it just seems to return an error even though I can manually create a delegate instance.

Private thisworksFine As New returnDel(AddressOf WorkWith_Return)

The error I get returned is

methodIf.CreateDelegate(GetType(returnDel2)) {System.ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type. at System.Reflection.RuntimeMethodInfo.CreateDelegateInternal(Type delegateType, Object firstArgument, DelegateBindingFlags bindingFlags, StackCrawlMark& stackMark) at System.Reflection.RuntimeMethodInfo.CreateDelegate(Type delegateType)} System.ArgumentException

This is the code

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

Can anyone help me by pointing out where I'm going wrong?

Upvotes: 1

Views: 336

Answers (1)

Trev
Trev

Reputation: 21

Just solved it this morning so I thought I would share the fix in case anyone else is having the same problem. I just had to add me and the delegate matches the one created with this line:

Private thisworksFine As New returnDel(AddressOf WorkWith_Return)

Working code:

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

Upvotes: 1

Related Questions