本文介绍了避免歧义匹配异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过反射对类型调用静态方法 Parse ,因为我在编译时不知道对象的类型(但是我知道,它具有 Parse 方法,使用字符串)。
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).
但是,我收到了一个模棱两可的匹配异常,大概是因为有很多重载的 Parse 每个方法都带有一个对象(字符串,int,double等)。
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.).
如何在方法调用中更具体地说明以确保达到正确的方法( Parse(string s)),并且不会引发异常。
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.
我的代码如下:
Type returnType = p.PropertyType; object value = returnType.GetMethod("Parse").Invoke(null, new string[] { "1" });推荐答案
使用此重载并使用
returnType.GetMethod("Parse", new [] {typeof(string)})更多推荐
避免歧义匹配异常
发布评论