I have a private method structure that I would like to call. Since I plan to do this in a performance-critical section, I would like to cache the delegate to perform the action. The problem is that I cannot bind to its method using Delegate.CreateDelegate. This structure is not my creation and is used in conjunction with a third-party library. The corresponding structure looks like this:
public struct A { private int SomeMethod() {
And the following code will fail with the error "Binding errors with the target method."
Delegate.CreateDelegate(typeof(Func<A,int>),typeof(A).GetMethod("SomeMethod",BindingFlags.Instance | BindingFlags.NonPublic));
I know that I can write an expression tree to perform an action, but it seems strange that I cannot use my usual goto method for these things with the Delegate.CreateDelegate
method.
The above code works fine if A
was a class. The problem arises only because A
is a structure. The MSDN documentation is incorrect for this CreateDelegate overload because it works with non-stationary methods.
source share