/// <summary> /// Maps form values from a NameValueCollection to an object. This is used specifically for auto mapping Form input values to an Object's properties. /// </summary> /// <param name="obj">The object to set the values on from the form.</param> /// <param name="form">Data from webform.</param> /// <param name="prefix">The prefix to use for the property names in the form.</param> /// <param name="excludeProps">The props to exclude.</param> public static void UpdateModel(object obj, NameValueCollection form, string prefix, IDictionary <string, string> excludeProps) { // 1. Only process public / settable properties BindingFlags flags = BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance; PropertyInfo[] props = obj.GetType().GetProperties(flags); foreach (var prop in props) { try { // 2. The name of the form input should be the same as the property name // Unless a prefix is supplied. // e.g. "Title" or "<prefix>.Title" as in "Post.Title" string name = prop.Name; string formname = string.IsNullOrEmpty(prefix) ? name : prefix + "." + name; string val = form[formname]; // 3. Now check that either // 1. property being processed is excluded from processing // 2. form input value does exist at all for this property ( val != null ). if (excludeProps == null || !excludeProps.ContainsKey(prop.Name) && val != null) { // 4. Allow the mapper helper to try to convert the value. // This is better because it uses conversion helpers to accomplish this. if (prop.PropertyType == typeof(string)) { prop.SetValue(obj, val, null); } else if (prop.PropertyType == typeof(bool) && val != null) { if (!string.IsNullOrEmpty(val) && val.Contains(",")) { val = val.Substring(0, val.IndexOf(",")); } MapperHelper.SetProperty(prop, obj, -1, null, val); } else if (prop.PropertyType == typeof(TimeSpan)) { MapperHelper.SetProperty(prop, obj, -1, null, val); } else if (TypeHelper.IsBasicType(prop.PropertyType) && val != null) { MapperHelper.SetProperty(prop, obj, -1, null, val); } } } catch (Exception) { } } }
/// <summary> /// Map the ini file /// </summary> /// <param name="errors">The errors.</param> /// <returns></returns> public IList <T> Map(IErrors errors) { IList <T> items = new List <T>(); // Check inputs. if (_doc == null || _doc.DocumentElement.ChildNodes == null || _doc.DocumentElement.ChildNodes.Count == 0) { return(items); } int counter = 0; var propMap = ReflectionUtils.GetPropertiesAsMap <T>(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, false); XmlElement rootNode = _doc.DocumentElement; XmlNodeList nodes = rootNode.ChildNodes; foreach (XmlNode node in nodes) { // Represents single object. T item = new T(); // Properties represented as xml child nodes. if (node.HasChildNodes) { foreach (XmlNode propNode in node.ChildNodes) { // Check if property exists string propName = propNode.Name.ToLower(); if (propMap.ContainsKey(propName)) { PropertyInfo prop = propMap[propName]; string val = propNode.InnerText; MapperHelper.SetProperty(prop, item, counter, errors, val); } } } items.Add(item); } return(items); }