I'm trying to call a generic method with a type detemirned at runtime. This call: Type type = ...; jToken.Value.Value<type>();
on a JToken object.
I've come this far, and I believe I'm almost there. But I'm getting a "parameter count mismatch" exception when calling Invoke
. I can't figure out why.
public void Foo(JObject obj, Type type)
{
foreach (var valuePair in obj)
{
JToken jToken = valuePair.Value;
var genericMethod = jToken.GetType().GetMethod("Value").MakeGenericMethod(new Type[] {type});
// should call jToken.Value<type>()
var value = genericMethod.Invoke(jToken, null);
}
}