8

I'm using reflection to find the generic method for Newtonsoft JsonConvert.DeserializedObject<T> but am finding that it's returning an ambiguous match for the non-generic version JsonConvert.DeserializeObject, here is the code which tries to get the generic method:

return typeof(JsonConvert).GetMethod("DeserializeObject", new Type[] { typeof(string) }).MakeGenericMethod(genericType);

I've specified that I want the method which takes a string as its only argument but both the generic and non-generic version have matching parameter lists, and I receive the ambiguous match error.

Is it possible to to get the generic version using GetMethod in this way? I know I can use Linq and GetMethods() to find it e.g:

var method = typeof(JsonConvert).GetMethods().FirstOrDefault(
            x => x.Name.Equals("DeserializeObject", StringComparison.OrdinalIgnoreCase) &&
            x.IsGenericMethod && x.GetParameters().Length == 1 &&
            x.GetParameters()[0].ParameterType == typeof(string));

But this is a bit cumbersome, there must be a better way.

DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • @DavidG - Hmm.. isn't the generic type passed in here `MakeGenericMethod(genericType);` ? – DGibbs Oct 24 '18 at 09:29
  • Sorry, misspoke in that comment. What is actually happening though is `GetMethod` cannot choose between `JsonConvert.DeserializeObject(string)` and `JsonConvert.DeserializeObject(string)`. I think you are stuck with the Linq method here. – DavidG Oct 24 '18 at 09:33
  • @DavidG - Yeah i was hoping there was some way to specify I want the generic version in `GetMethod()` but I guess not, thanks anyway! – DGibbs Oct 24 '18 at 09:37

4 Answers4

4

I think you want this:

var method = typeof(JsonConvert).GetMethods().FirstOrDefault(
    x => x.Name.Equals("DeserializeObject", StringComparison.OrdinalIgnoreCase) &&
    x.IsGenericMethod && x.GetParameters().Length == 1)
    ?.MakeGenericMethod(genericType);
Spixy
  • 303
  • 4
  • 11
  • 1
    This is exactly what I have in the OP - minus the param type check, I'm looking for a way to do it with `GetMethod()` – DGibbs Oct 24 '18 at 09:29
  • I don´t see exactly this one (4th line is different) – Spixy Oct 24 '18 at 09:30
  • Ok, it's a subtle tweak but my point is, it's the same solution I already have in the OP which works, but id like to do it with `GetMethod()` if at all possible. – DGibbs Oct 24 '18 at 09:34
  • 1
    @DGibbs there is no way using `GetMethod`, you have to use `GetMethods` and filter afterwards. – thehennyy Oct 24 '18 at 09:34
  • updated with genericType so this one will give you `JsonConvert.DeserializeObject(string)` – Spixy Oct 24 '18 at 09:40
3

You can derive from Binder class

class MyBinder : Binder
{
    public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
    {          
        return match.First(m => m.IsGenericMethod);
    }

    #region not implemented
    public override MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state) => throw new NotImplementedException();
    public override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture) => throw new NotImplementedException();
    public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers) => throw new NotImplementedException();
    public override object ChangeType(object value, Type type, CultureInfo culture) => throw new NotImplementedException();
    public override void ReorderArgumentArray(ref object[] args, object state) => throw new NotImplementedException();
    #endregion
}

Usage:

var method = typeof(JsonConvert).GetMethod("DeserializeObject",
    BindingFlags.Public | BindingFlags.Static,
    new MyBinder(),
    new[] {typeof(string)},
    null);

In your case MyBinder will receive two candidates in SelectMethod

public static object DeserializeObject(string value)
public static T DeserializeObject<T>(string value)

Code above will select first generic method

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
3

There are GetMethod methods which do distinguish between generic and non-generic methods; an example is this one with the integer parameter genericParamCount:

Searches for the specified public method whose parameters match the specified generic parameter count and argument types.

The following code:

var genericMethod = typeof(JsonConvert)
    .GetMethod("DeserializeObject", 1, new Type[] { typeof(string) })
    .MakeGenericMethod(typeof(string));
var otherMethod = typeof(JsonConvert)
    .GetMethod("DeserializeObject", 0, new Type[] { typeof(string) });
Console.WriteLine(genericMethod);
Console.WriteLine(otherMethod);

will print

System.String DeserializeObject[String](System.String)
System.Object DeserializeObject(System.String)

so this is perhaps the shortest solution to your problem.

jamesmule
  • 3
  • 2
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
2

The problem is that Type.GetMethod doesn't allow you to specify the generic type meaning that this code:

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

is trying to resolve between the 2 matching methods JsonConvert.DeserializeObject(string) and JsonConvert.DeserializeObject<T>(string).

Unfortunately you are stuck with the Linq option to do this.

DavidG
  • 113,891
  • 12
  • 217
  • 223