0

I am trying to get the property names and their corresponding values of an unknown object.

This is what I came up with as a test, but I am getting a "Parameter Count Mismatch" error when calling prop.GetValue(myObj)

FundingPastDueEvent _event = new FundingPastDueEvent()
{
    InvoiceCount = 123
};

var nsJson = JsonConvert.SerializeObject(_event);

var myObj = JsonConvert.DeserializeObject<object>(nsJson);

var properties = myObj.GetType().GetProperties();

foreach (var prop in properties)
{
    Console.WriteLine($"{prop.Name}: {prop.GetValue(myObj)}");
}
Adjit
  • 10,134
  • 12
  • 53
  • 98
  • 1
    Is one of the properties indexed? See the method signatures for indexed properties here: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.getvalue?view=net-6.0 – adv12 Apr 07 '22 at 20:07
  • 1
    See PropertyInfo.GetIndexParameters: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.getindexparameters?view=net-6.0 – adv12 Apr 07 '22 at 20:08
  • @adv12 no, not indexed. You can see the object in the example `FundingPastDueEvent` – Adjit Apr 07 '22 at 20:24
  • So `FundingPastDueEvent` just has one member called `InvoiceCount` having a type of `int`? – Robert Harvey Apr 07 '22 at 20:36
  • OK, let's see the contents of `nsJson`. – Robert Harvey Apr 07 '22 at 20:36
  • 1
    @RobertHarvey OP is checking properties of Newtonsoft.Json.Linq.JObject (you can ignore type they have as a sample), indeed it has indexed properties. – Alexei Levenkov Apr 07 '22 at 20:40
  • See [this answer](https://stackoverflow.com/a/24645588/2557128) on how `DeserializeObject` handles `object` as the target type – NetMage Apr 07 '22 at 21:21

0 Answers0