Fetch list of property name and value from any object

In any of the scenario where we have to wrote a common function where we can use any of the object type but all have set of common property so we need to fetch those properties.

In one of the my scenario where I am creating common data log function where what ever object I pass to this function and that function is saving all properties value in database log table with property and value.
This kind of logic we can say as reflation. Below is the sample code for this.

public void GetPropertiesFromObject(object oObject)
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(oObject))
                string name = descriptor.Name;
                string value = "";
                if (descriptor.GetValue(oObject) != null)
                    value = descriptor.GetValue(oObject).ToString();
                //use this name and value
                if (descriptor.Attributes[4].GetType().Name == "DataMemberAttribute")
                    this.InsertRequesObjectParameter(this.ID, name, value, false, 1);

To run this code need to use below namespace

We can get all other attributes and function name as well as by this also we can able to call function as well.

Amit Patel
“Enjoy Programming”


Get every new post delivered to your Inbox.

Join 242 other followers