I have a struct with a private method that I'd like to invoke. Since I plan to do this in a performance critical section, I'd like to cache a delegate to perform the action. The problem is I can't seem to bind to its method with Delegate.CreateDelegate. The struct in question is not my creation and is used in interaction with a third party library.
The struct in question looks like this::
public struct A
{
private int SomeMethod()
{
//body go here
}
}
And the following code will fail with an "Error binding to target method".
Delegate.CreateDelegate(typeof(Func<A,int>),typeof(A).GetMethod("SomeMethod",BindingFlags.Instance | BindingFlags.NonPublic));
I know I can write an expression tree to perform the action, but it seems odd that I can't use my normal goto for these things the Delegate.CreateDelegate
method.
The above code works just fine if A
were a class. The issue only arises because A
is a struct.
MSDN documentation is incorrect for this overload of CreateDelegate as it does work on non-static methods.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…