How to get property names and value using .NET

How to get property value from dynamic object in .NET

Here is how you can get property name and value from the dynamic object using C#.

let’s assume the dynamic object as:

dynamic objectDetails = new {“Name” : “Ravindra Naik”, “Age” : 26, “Gender”: “Male”}

Now to fetch all the properties present in “objectDetails” here is what you need to do:

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

In order to fetch a list of property name and values, here is what you need to do:

foreach(var prop in properties)

{

var propName = prop.Name;

var propValue = objectDetails[propName];

}

Here is how you can get property names and values inside a dynamic object using .NET

 

Thanks for dropping by !! Feel free to comment to this post or you can also drop me an email at naik899@gmail.com

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *