0

I am currently learning how to do reflection and I have been succesful with regards to getting the propeties and values of a stringly typed class. However, when I try with a dynamic object, I get an exception:

System.Reflection.TargetParameterCountException: Parameter count mismatch.

I've tried some of the solutions (e.g. object foo = dynamic obj then using obj) here but none seem to work because they don't quite reflect my problem.

Here is my code:

dynamic evtPc1 = JsonConvert.DeserializeObject(json);

PropertyInfo[] properties = evtPc1.GetType().GetProperties();

for (int i = 0; i < properties.Length; i++)
{
    Console.WriteLine($"Property: {properties[i].GetValue(evtPc1)}");
}
user1574598
  • 3,771
  • 7
  • 44
  • 67
  • Per the [docs](https://learn.microsoft.com/dotnet/api/system.reflection.propertyinfo.getvalue): "You call the `GetValue(Object)` overload to retrieve the value of a non-indexed property; if you try to retrieve the value of an indexed property, the method throws a `TargetParameterCountException` exception. You can determine whether a property is indexed or not by calling the `GetIndexParameters` method. If the length of the returned `ParameterInfo` array is zero, the property is not indexed." – Jeroen Mostert Jan 17 '20 at 12:16
  • Maybe this can help : https://stackoverflow.com/questions/2634858/how-do-i-reflect-over-the-members-of-dynamic-object Also, I would suggest using JsonSerializer instead of JSON.NET. Please see the new methods here : https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to – rak007 Jan 17 '20 at 12:18

1 Answers1

0

If you really want properties of deserialized JSON you could do what docs says, though you may be surprised with the output:

dynamic evtPc1 = JsonConvert.DeserializeObject(json);
PropertyInfo[] properties = evtPc1.GetType().GetProperties();
for (int i = 0; i < properties.Length; i++)
{
    var prop = properties[i];

    if (prop.GetIndexParameters().Length == 0)
        Console.WriteLine("{0} ({1}): {2}", prop.Name,
            prop.PropertyType.Name,
            prop.GetValue(evtPc1));
    else
        Console.WriteLine("{0} ({1}): <Indexed>", prop.Name,
            prop.PropertyType.Name);
}

But if you want get only properties from JSON itself, this is a bit more tricky (not sure if this code is optimal though). Note, this code doesn't cover case of nested JSON objects:

dynamic evtPc1 = JsonConvert.DeserializeObject(json);
PropertyInfo[] properties = evtPc1.GetType().GetProperties();
for (int i = 0; i < properties.Length; i++)
{
    var prop = properties[i];

    if (prop.Name == nameof(JToken.First) && prop.PropertyType.Name == nameof(JToken))
    {
        var token = (JToken) prop.GetValue(evtPc1);
        while (token != null)
        {
            if (token is JProperty castProp)
                Console.WriteLine($"Property: {castProp.Name}; Value: {castProp.Value}");

            token = token.Next;
        }
    }
}
Alexander Goldabin
  • 1,824
  • 15
  • 17