Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ambiguous match when calling method via reflection

Tags:

c#

reflection

When I try to call JsonConvert.DeserialiseObject via reflection I get an AmbiguousMatchException despite me specifying the type of the parameter for the overload I want to call

MethodInfo method = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string) });

Not sure what other info I can supply so that it finds a unique match

any ideas?

like image 549
ChrisCa Avatar asked Mar 24 '16 16:03

ChrisCa


1 Answers

As mentioned, you can use the GetMethods() method with Linqs Single() method to find the MethodInfo you are looking for:

var method = typeof (JsonConvert).GetMethods().Single(
            m =>
                m.Name == "DeserializeObject" &&
                m.GetGenericArguments().Length == 1 &&
                m.GetParameters().Length == 1 &&
                m.GetParameters()[0].ParameterType == typeof(string));
like image 185
thehennyy Avatar answered Nov 02 '22 23:11

thehennyy