通过反射调用方法时的模糊匹配

Chr*_*sCa 4 c# reflection

当我尝试JsonConvert.DeserialiseObject通过反射调用时,AmbiguousMatchException尽管我指定了要调用的重载参数的类型,但我得到了一个

MethodInfo method = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string) });
Run Code Online (Sandbox Code Playgroud)

不确定我能提供什么其他信息,以便找到一个独特的匹配

有任何想法吗?

the*_*nyy 8

如上所述,您可以使用GetMethods()Linqs Single()方法的方法来查找您要查找的MethodInfo:

var method = typeof (JsonConvert).GetMethods().Single(
            m =>
                m.Name == "DeserializeObject" &&
                m.GetGenericArguments().Length == 1 &&
                m.GetParameters().Length == 1 &&
                m.GetParameters()[0].ParameterType == typeof(string));
Run Code Online (Sandbox Code Playgroud)