Avoiding an ambiguous match exception

青春壹個敷衍的年華 提交于 2019-11-27 00:25:18

问题


I am invoking a static method Parse on a type via reflection because I do not know the type of the object at compile-time (I do know, however, it has a Parse method, taking a string).

However, I am getting an ambiguous match exception, presumably because there are a lot of overloaded Parse methods each taking a single object (string, int, double etc.).

How can I be more specific in my method invocation to ensure I reach the correct method (Parse(string s)) and the exception is not thrown.

My code looks like this:

Type returnType = p.PropertyType;
object value = returnType.GetMethod("Parse").Invoke(null, new string[] { "1" });

回答1:


Use this overload and use

returnType.GetMethod("Parse", new [] {typeof(string)})


来源:https://stackoverflow.com/questions/1969411/avoiding-an-ambiguous-match-exception

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