예제 #1
0
        /// <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 (_data == null || _data.Count == 0)
            {
                return(items);
            }

            int counter        = 0;
            var propMapDefault = ReflectionUtils.GetPropertiesAsMap <T>(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, false);

            foreach (DictionaryEntry entry in _data)
            {
                // Represents single object.
                if (entry.Value is IDictionary)
                {
                    Tuple2 <T, IDictionary <string, PropertyInfo> > tuple = GetNewObjectAndPropertyMapAsTuple((IDictionary)entry.Value, propMapDefault);
                    MapperHelper.MapTo <T>(tuple.First, counter, tuple.Second, entry.Value as IDictionary);
                    items.Add(tuple.First);
                    counter++;
                }
                // Multiple sections with the same name ( "post", "post" )
                else if (entry.Value is List <object> )
                {
                    List <object> sections = entry.Value as List <object>;
                    foreach (object section in sections)
                    {
                        if (section is IDictionary)
                        {
                            Tuple2 <T, IDictionary <string, PropertyInfo> > tuple = GetNewObjectAndPropertyMapAsTuple((IDictionary)section, propMapDefault);
                            MapperHelper.MapTo <T>(tuple.First, counter, tuple.Second, section as IDictionary);
                            items.Add(tuple.First);
                            counter++;
                        }
                    }
                }
            }
            return(items);
        }
예제 #2
0
        /// <summary>
        /// Map the ini file
        /// </summary>
        /// <param name="errors"></param>
        /// <returns></returns>
        public IList <T> Map(IErrors errors)
        {
            IList <T> items = new List <T>();

            // Check inputs.
            if (_doc == null || _doc.Columns.Count == 0)
            {
                return(items);
            }

            var propMap = ReflectionUtils.GetPropertiesAsMap <T>(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, false);
            int counter = 0;

            foreach (OrderedDictionary record in _doc.Data)
            {
                // Represents single object.
                T item = new T();
                MapperHelper.MapTo <T>(item, counter, propMap, record as IDictionary);
                items.Add(item);
                counter++;
            }
            return(items);
        }
예제 #3
0
        /// <summary>
        /// Map the properties in the data to the properties of the item T using the propMap supplied.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="counterOrRefId">An counter to help associate errors in mapping w/ a specific item index or reference id.</param>
        /// <param name="item">The object to set the properties on.</param>
        /// <param name="propMap">Property map containing the names of the properties that can be mapped.</param>
        /// <param name="data">The source of the data to map.</param>
        /// <param name="errors">Error collection.</param>
        public static void MapTo <T>(T item, int counterOrRefId, IDictionary <string, PropertyInfo> propMap, IDictionary data, IErrors errors)
        {
            var handledProps = new Dictionary <string, bool>();

            foreach (DictionaryEntry entry in data)
            {
                var          propname = entry.Key as string;
                var          val      = entry.Value;
                PropertyInfo prop     = null;
                if (propMap.ContainsKey(propname))
                {
                    prop = propMap[propname];
                    SetProperty(prop, item, counterOrRefId, errors, val);
                }
                else if (propMap.ContainsKey(propname.ToLower().Trim()))
                {
                    prop = propMap[propname.Trim().ToLower()];
                    SetProperty(prop, item, counterOrRefId, errors, val);
                }
                else if (propname.Contains("."))
                {
                    string objectname = propname.Substring(0, propname.IndexOf("."));
                    if (!handledProps.ContainsKey(objectname))
                    {
                        if (propMap.ContainsKey(objectname.ToLower()))
                        {
                            prop = propMap[objectname.ToLower().Trim()];
                            // Composite object.
                            object obj = Activator.CreateInstance(prop.PropertyType);
                            prop.SetValue(item, obj, null);
                            MapperHelper.MapTo(obj, data, objectname, errors);
                            handledProps[objectname] = true;
                        }
                    }
                }
            }
        }