John Bustos
John Bustos

Reputation: 19544

See all an object's properties and values (including child objects)

I was wondering if there was a built in feature in .Net that could output (at runtime) all the properties and values in a given object and, if possible, even including sub-objects.

I'm thinking of Reflection or XML Serializion and saw some people mentioned JSON, but can't really figure out how to do it the right way... Does anyone know if this is possibly a built in feature in .Net or if there's a good example / tool that either already does this or could guide me in the right direction?

Upvotes: 0

Views: 149

Answers (2)

tweakch
tweakch

Reputation: 426

You could use the XmlSerializer class or take a look at the JSON.Net framework.

Upvotes: 1

Mohammed Hossain
Mohammed Hossain

Reputation: 1319

Use Reflection.

To see all the public properties and values of an object:

foreach(var prop in obj.GetType().GetProperties()) //note: you can pass in binding flags to GetProperties to get static, private, etc properties
{
    var propVal = prop.GetValue(obj);

    //prop has information such as Name, PropertyType
    //propVal is the value of that property for obj
}

Upvotes: 3

Related Questions