Monday, October 19, 2015

Get the values of all fields/properties of a C# object using reflection

using System.Reflection;     


 private List GetPropertyList(object obj)
        {
            List propertyList = new List();

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance |
                                                            BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                object o = property.GetValue(obj, null);
                propertyList.Add(o == null ? "" : o.ToString());
            }
            return propertyList;
        }

No comments: