示例#1
0
 public static void SetProperty(object obj, PropertyInfo prop, string propVal)
 {
     Guard.IsNotNull(obj, "Object containing properties to set is null");
     Guard.IsNotNull(prop, "Property not supplied.");
     if (prop != null && prop.CanWrite && ReflectionTypeChecker.CanConvertToCorrectType(prop, propVal))
     {
         object value = ReflectionTypeChecker.ConvertToSameType(prop, propVal);
         prop.SetValue(obj, value, null);
     }
 }
示例#2
0
        public static void SetProperty <T>(object obj, string propName, object propVal) where T : class
        {
            Guard.IsNotNull(obj, "Object containing properties to set is null");
            Guard.IsTrue(!string.IsNullOrEmpty(propName), "Property name not supplied.");
            propName = propName.Trim();
            if (string.IsNullOrEmpty(propName))
            {
                throw new ArgumentException("Property name is empty.");
            }
            Type         type     = obj.GetType();
            PropertyInfo property = type.GetProperty(propName);

            if (property != null && property.CanWrite && ReflectionTypeChecker.CanConvertToCorrectType(property, propVal))
            {
                object value = ReflectionTypeChecker.ConvertToSameType(property, propVal);
                property.SetValue(obj, value, null);
            }
        }
示例#3
0
 public static bool CanConvertTo <T>(string val)
 {
     return(ReflectionTypeChecker.CanConvertTo(typeof(T), val));
 }