1 year ago

#389034

test-img

Adjit

Reflect an unknown object to get property names and values C#

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

c#

reflection

0 Answers

Your Answer

Accepted video resources