Reputation: 3995
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
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