/// <summary>
        /// Mapping配置实体类型。
        /// </summary>
        /// <param name="type"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public object Mapping(Type type, XElement element, IConvertCaseStrategy convertCaseStrategy)
        {
            // 获取mappinger选择器
            IMappinger mappinger = MappingerSelector.Get(type, convertCaseStrategy);

            convertCaseStrategy = convertCaseStrategy ?? new DefaultConvertCaseStrategy();

            // 如果不是entity,直接执行返回值
            if (!(mappinger is EntityMappinger))
            {
                mappinger.ConvertCaseStrategy = convertCaseStrategy;
                return(mappinger.Mapping(element, type));
            }


            // 创建实例
            var instance = Activator.CreateInstance(type);

            // 创建 mapping
            ITypeMapping typeMapping = MappingFactory.CreateMapping(type, element, convertCaseStrategy);

            // 执行
            typeMapping.Apply(instance);
            return(instance);
        }
 public void Apply(object instance)
 {
     try
     {
         var value = _mappinger.Mapping(_element, _property.PropertyType);
         _property.SetValue(instance, value, null);
     }
     catch (Exception ex)
     {
         string msg = $"转换失败,element:{_element.Name.LocalName},value:{_element.Value}。在类型:{instance.GetType()}属性:{_property.Name}。更多详情请参考inner exception.";
         throw new SuperConfigException(msg, ex);
     }
 }
Exemplo n.º 3
0
        public object Mapping(XElement element, Type type)
        {
            var listType  = typeof(List <>);
            var arrayType = type.GetElementType();

            listType = listType.MakeGenericType(arrayType);

            IMappinger mappinger = MappingerSelector.Get(listType, ConvertCaseStrategy);

            var list = mappinger.Mapping(element, listType);


            return((Array)listType.GetMethod("ToArray").Invoke(list, null));
        }
        /// <summary>
        /// 获取key value值
        /// </summary>
        /// <param name="element"></param>
        /// <param name="mappinger"></param>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private object GetValue(XElement element, IMappinger mappinger, Type type, string name)
        {
            var attribute = element.Attributes().Where(i => i.Name == name).FirstOrDefault();

            if (attribute != null)
            {
                return(attribute.Value);
            }

            var ele = element.Elements().Where(i => i.Name == name).FirstOrDefault();

            if (ele != null)
            {
                return(mappinger.Mapping(ele, type));
            }

            return(null);
        }