Quantcast
Channel: amitpatelit » .NET
Viewing all articles
Browse latest Browse all 11

Fetch list of property name and value from any object

$
0
0

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
System.ComponentModel;

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

Thanks,
Amit Patel
“Enjoy Programming”
amitpatel.it@gmail.com



Viewing all articles
Browse latest Browse all 11

Trending Articles