コード例 #1
0
        public static ITypeMapping CreateMapping(Type type, XElement element, IConvertCaseStrategy convertCaseStrategy)
        {
            var elementList   = element.Elements().ToList();
            var attributeList = element.Attributes().ToList();

            var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanWrite && p.GetSetMethod(true).IsPublic).ToList();

            ITypeMapping typeMapping = new TypeMapping();

            foreach (var item in properties)
            {
                var xName = convertCaseStrategy.ConvertCase(item.Name);

                var attribute = attributeList.Where(i => i.Name == xName).FirstOrDefault();
                if (attribute != null)
                {
                    var mappingAttributes = new MappingForAttributes(attribute, item);
                    typeMapping.Add(mappingAttributes);
                    continue;
                }

                var ele = elementList.Where(i => i.Name == xName).FirstOrDefault();
                if (ele != null)
                {
                    var mappinger      = MappingerSelector.Get(item.PropertyType, convertCaseStrategy);
                    var mappingElement = new MappingForElement(ele, item, mappinger);
                    typeMapping.Add(mappingElement);
                }
            }

            return(typeMapping);
        }
コード例 #2
0
        /// <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);
        }