/// <summary> /// Gets the readable (non indexed) properties names and values. /// The keys holds the names of the properties. /// The values are the values of the properties /// </summary> public static IDictionary GetPropertiesDictionary(object obj) { object propertyValue = null; var ht = new Hashtable(); foreach (PropertyInfo property in obj.GetType(). GetProperties(BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic)) { if (property.CanRead && property.GetIndexParameters().Length == 0) { if (ReflectionUtil.IsSimpleType(property.PropertyType)) { propertyValue = property.GetValue(obj, null); ht[property.Name] = (propertyValue == null ? null : propertyValue.ToString()); } else { ht[property.Name] = property.GetValue(obj, null); } } } return(ht); }
/// <summary> /// Gets the property value. /// </summary> /// <param name="property">The property.</param> /// <param name="obj">The obj.</param> /// <returns></returns> public static object GetPropertyValue(PropertyInfo property, object obj) { if (property.CanRead && property.GetIndexParameters().Length == 0) { if (ReflectionUtil.IsSimpleType(property.PropertyType)) { return(property.GetValue(obj, null)); } else { return(property.GetValue(obj, null)); } } return("{indexed or write only property}"); }
/// <summary> /// Gets the fields names and values. /// The keys holds the names of the fields. /// The values hold the value of the field if it's a simple type, /// or the name of the field's type. /// </summary> public static IDictionary GetFieldsDictionary(object obj) { var ht = new Hashtable(); foreach (FieldInfo field in obj.GetType().GetFields()) { if (ReflectionUtil.IsSimpleType(field.FieldType)) { ht[field.Name] = field.GetValue(obj); } else { ht[field.Name] = field.FieldType.Name; } } return(ht); }