SamFisher83
SamFisher83

Reputation: 3995

Error binding to target method

MethodInfo method = typeof(T).GetMethod("Parse", new[] { typeof(string) });
parse = Delegate.CreateDelegate(typeof(Func<T,string>), method);

T is a float in this case. However I am getting a Error binding to target method. Parse I believe is a static method. I have looked at other examples, but I can not figure out why it is not binding.

Upvotes: 2

Views: 2240

Answers (1)

Davide Piras
Davide Piras

Reputation: 44605

you have to swap T and string because the method returns a T not a string.

I replaced T with float and following code works for me:

MethodInfo method = typeof(float).GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string) }, null);

var parse = Delegate.CreateDelegate(typeof(Func<string, float>), method);

source: VS intellisense and MSDN Func(Of T, TResult) Delegate

Upvotes: 3

Related Questions