Beispiel #1
0
        // Safe method
        public static T GetValue <T>(this object obj, string propertyName)
        // where T : IConvertible
        {
            if (obj is ExpandoObject || obj is IDictionary <string, object> )
            {
                var    dict  = obj as IDictionary <string, object>;
                object value = null;
                dict.TryGetValue(propertyName, out value);
                return((T)value);
            }

            PropertyDescriptor pi = ReflectionCache.GetProperty(obj, propertyName);

            if (pi == null)    //  || !pi.CanRead // .CanWrite
            {
                return(default(T));
            }

            return((T)System.Convert.ChangeType(obj.GetPropertyValue(propertyName), pi.PropertyType));
        }
Beispiel #2
0
 public static IEnumerable <PropertyDescriptor> Properties(this object obj)
 {
     return(ReflectionCache.GetProperties(obj));
 }