ChrisCa
ChrisCa

Reputation: 11056

ambiguous match when calling method via 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?

Upvotes: 6

Views: 7174

Answers (1)

thehennyy
thehennyy

Reputation: 4216

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));

Upvotes: 14

Related Questions