Error binding to target method

扶醉桌前 提交于 2019-12-24 04:05:29

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/7814156/error-binding-to-target-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!