Ejemplo n.º 1
0
 /// <summary>
 /// Maps values from attribute to mapping object
 /// </summary>
 /// <param name="attribute">Property mapping attribute</param>
 /// <param name="mapping">Property mapping object</param>
 private static void MapBasePropertyValues(PropertyMappingAttribute attribute, IPropertyMapping mapping)
 {
     mapping.SourceProperty                   = attribute.SourceProperty;
     mapping.LevelsAbove                      = attribute.LevelsAbove;
     mapping.SourceChildProperty              = attribute.SourceChildProperty;
     mapping.SourceRelatedProperty            = attribute.SourceRelatedProperty;
     mapping.ConcatenationSeperator           = attribute.ConcatenationSeperator;
     mapping.SourcePropertiesForCoalescing    = attribute.SourcePropertiesForCoalescing;
     mapping.SourcePropertiesForConcatenation = attribute.SourcePropertiesForConcatenation;
     mapping.DefaultValue                     = attribute.DefaultValue;
     mapping.DictionaryKey                    = attribute.DictionaryKey;
     mapping.Ignore = attribute.Ignore;
     mapping.PropertyValueGetter = attribute.PropertyValueGetter;
     mapping.MapRecursively      = attribute.MapRecursively;
     mapping.FallbackMethods     = attribute.FallbackMethods;
     mapping.CustomMappingType   = attribute.CustomMappingType;
     mapping.CustomMappingMethod = attribute.CustomMappingMethod;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Helper to convert the values of a property mapping attribute to an instance of PropertyMapping
        /// </summary>
        /// <param name="attribute">Attribute added to a view model property</param>
        /// <returns>PropertyMapping instance</returns>
        protected static PropertyMapping GetPropertyMappingAttributeAsPropertyMapping(PropertyMappingAttribute attribute)
        {
            var mapping = new PropertyMapping();

            MapBasePropertyValues(attribute, mapping);
            return(mapping);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read object from the node.
        /// </summary>
        /// <param name="type">The object's type.</param>
        /// <param name="xmlNode">The node.</param>
        /// <returns>The object.</returns>
        public object ReadObject(Type type, XmlNode xmlNode)
        {
            //if (type.Name.ToLower() != xmlNode.Name.ToLower())
            //    throw new InvalidCastException("Cannot convert " + xmlNode.Name + " into the " + type.Name);

            object obj = Activator.CreateInstance(type);

            PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            Dictionary <string, PropertyInfo>           attributes       = new Dictionary <string, PropertyInfo>();
            Dictionary <string, ICustomMappingStrategy> customStrategies = new Dictionary <string, ICustomMappingStrategy>();
            List <string> ignoreList = new List <string>();

            IEnumerable <PropertyInfo> props = type.GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(PropertyMappingAttribute)));

            foreach (PropertyInfo propertyInfo in props)
            {
                PropertyMappingAttribute attr = (PropertyMappingAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(PropertyMappingAttribute));
                attributes.Add(attr.Name.ToLower(), propertyInfo);
            }

            props = type.GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(CustomStrategyAttribute)));
            foreach (PropertyInfo propertyInfo in props)
            {
                CustomStrategyAttribute attr     = (CustomStrategyAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(CustomStrategyAttribute));
                ICustomMappingStrategy  strategy = (ICustomMappingStrategy)Activator.CreateInstance(attr.StrategyType);
                customStrategies.Add(propertyInfo.Name.ToLower(), strategy);
            }

            props = type.GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(IgnorePropertyAttribute)));
            foreach (PropertyInfo propertyInfo in props)
            {
                ignoreList.Add(propertyInfo.Name.ToLower());
            }

            if (xmlNode.Attributes != null)
            {
                for (int i = 0; i < xmlNode.Attributes.Count; i++)
                {
                    XmlAttribute xmlAttribute   = xmlNode.Attributes[i];
                    string       attributeName  = xmlAttribute.Name.ToLower();
                    string       attributeValue = xmlAttribute.Value;

                    if (ignoreList.Contains(attributeName))
                    {
                        continue;
                    }

                    PropertyInfo propertyInfo;
                    if (attributes.TryGetValue(attributeName, out propertyInfo) == false)
                    {
                        propertyInfo = propertyInfos.Single(x => x.Name.ToLower() == attributeName);
                    }

                    object value;
                    if (customStrategies.ContainsKey(propertyInfo.Name.ToLower()))
                    {
                        ICustomMappingStrategy customMappingStrategy = customStrategies[propertyInfo.Name.ToLower()];
                        value = customMappingStrategy.Map(attributeValue, propertyInfo.PropertyType);
                    }
                    else
                    {
                        IPrimitiveMappingStrategy mappingStrategy = mappingStrategyFactory.CreatePrimitiveStrategy(propertyInfo.PropertyType);
                        value = mappingStrategy.Map(attributeValue, propertyInfo.PropertyType);
                    }
                    propertyInfo.SetValue(obj, value, null);
                }
            }

            if (xmlNode.HasChildNodes)
            {
                for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
                {
                    XmlNode child = xmlNode.ChildNodes[i];
                    if (child.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    string childName = child.Name.ToLower();
                    if (ignoreList.Contains(childName))
                    {
                        continue;
                    }

                    PropertyInfo propertyInfo;
                    if (attributes.TryGetValue(childName, out propertyInfo) == false)
                    {
                        propertyInfo = propertyInfos.Single(x => x.Name.ToLower() == childName);
                    }

                    if (customStrategies.ContainsKey(propertyInfo.Name.ToLower()))
                    {
                        ICustomMappingStrategy customMappingStrategy = customStrategies[propertyInfo.Name.ToLower()];
                        var value = customMappingStrategy.Map(child, propertyInfo.PropertyType);
                        propertyInfo.SetValue(obj, value, null);
                        continue;
                    }

                    Type propertyType = propertyInfo.PropertyType;
                    if (propertyType.IsPrimitive || propertyType == typeof(string) || propertyType.IsEnum)
                    {
                        IPrimitiveMappingStrategy mappingStrategy = mappingStrategyFactory.CreatePrimitiveStrategy(propertyInfo.PropertyType);
                        var value = mappingStrategy.Map(child.InnerText, propertyInfo.PropertyType);
                        propertyInfo.SetValue(obj, value, null);
                    }
                    else
                    {
                        IMappingStrategy mappingStrategy = mappingStrategyFactory.CreateComplexStrategy(propertyInfo.PropertyType);
                        var value = mappingStrategy.Map(child, propertyInfo.PropertyType, this);
                        propertyInfo.SetValue(obj, value, null);
                    }
                }
            }

            return(obj);
        }