Ejemplo n.º 1
0
 /// <summary>
 /// Set the property using the value provided.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="prop"></param>
 /// <param name="item"></param>
 /// <param name="counterOrRefId"></param>
 /// <param name="errors"></param>
 /// <param name="val"></param>
 public static void SetProperty(PropertyInfo prop, object item, int counterOrRefId, IErrors errors, object val)
 {
     try
     {
         // Found prop. Can now map.
         if (prop != null)
         {
             object convertedVal = null;
             // Handle special conversions. e.g. $105 or 9am etc.
             if (_propertyTypeMappers.ContainsKey(prop.PropertyType) && val != null && val.GetType() == typeof(string))
             {
                 Func <string, object> converter = _propertyTypeMappers[prop.PropertyType];
                 convertedVal = converter((string)val);
             }
             else
             {
                 convertedVal = ReflectionTypeChecker.ConvertToSameType(prop, val);
             }
             ReflectionUtils.SetProperty(item, prop, convertedVal, true);
         }
     }
     catch (Exception ex)
     {
         if (errors != null)
         {
             var err = string.Format("Unable to map property '{0}' for counter/refId '{1}'", prop.Name, counterOrRefId);
             err += Environment.NewLine + ex.Message;
             errors.Add(err);
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Maps all the keys/values in the data dictionary to the
        /// </summary>
        /// <param name="object">The object to map</param>
        /// <param name="data">The data to map to the object.</param>
        /// <param name="namefilter">Filter on the keys. e.g. "Location." will only map keys that contain "Location."</param>
        /// <param name="errors">Error list for collecting errors.</param>
        public static void MapTo(object obj, IDictionary data, string namefilter, IErrors errors)
        {
            // 1. Get all the public, instance, writable properties.
            Type type    = obj.GetType();
            var  propMap = ReflectionUtils.GetPropertiesAsMap(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, false);

            // 2. Making keys/filter case insensitive. Location. = location.
            namefilter = namefilter.Trim().ToLower();

            // 3. Iterate through all the keys.
            foreach (DictionaryEntry entry in data)
            {
                var keyname = entry.Key as string;
                keyname = keyname.Trim().ToLower();
                PropertyInfo prop = null;

                // 4. key in data matches filter?
                if (keyname.Contains(namefilter))
                {
                    // Get "City" from "Location.City";
                    string propname = keyname.Substring(keyname.IndexOf(".") + 1);
                    propname.Trim().ToLower();

                    // 5. propname exists in the data type?
                    if (propMap.ContainsKey(propname))
                    {
                        // 6. Finally map
                        prop = propMap[propname];
                        object val          = entry.Value;
                        object valConverted = ReflectionTypeChecker.ConvertToSameType(prop, val);
                        prop.SetValue(obj, valConverted, null);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validates various aspects of the argument.
        /// </summary>
        /// <param name="argAttr"></param>
        /// <param name="argVal"></param>
        /// <param name="errors"></param>
        public static bool ValidateArg(ArgAttribute argAttr, string argVal, IList <string> errors)
        {
            // Arg name or index.
            string argId             = string.IsNullOrEmpty(argAttr.Name) ? "[" + argAttr.IndexPosition + "]" : argAttr.Name;
            int    initialErrorCount = errors.Count;

            // Argument missing and required.
            if (argAttr.IsRequired && string.IsNullOrEmpty(argVal))
            {
                errors.Add(string.Format("Required argument '{0}' : {1} is missing.", argAttr.Name, argAttr.DataType.FullName));
                return(false);
            }

            // Argument missing and Optional - Can't do much.
            if (!argAttr.IsRequired && string.IsNullOrEmpty(argVal))
            {
                return(true);
            }

            // File doesn't exist.
            if (argAttr.DataType == typeof(System.IO.File) && !System.IO.File.Exists(argVal))
            {
                errors.Add(string.Format("File '{0}' associated with argument '{1}' does not exist.", argVal, argId));
            }

            // Wrong data type.
            else if (!argAttr.Interpret && !ReflectionTypeChecker.CanConvertTo(argAttr.DataType, argVal))
            {
                errors.Add(string.Format("Argument value of '{0}' for '{1}' does not match type {2}.",
                                         argVal, argId, argAttr.DataType.FullName));
            }

            return(initialErrorCount == errors.Count);
        }