예제 #1
0
        public TypeMap CreateTypeMap(Type sourceType, Type destinationType, IMappingOptions options, MemberList memberList)
        {
            var sourceTypeInfo = GetTypeInfo(sourceType);
            var destTypeInfo = GetTypeInfo(destinationType);

            var typeMap = new TypeMap(sourceTypeInfo, destTypeInfo, memberList);

            foreach (var destProperty in destTypeInfo.GetPublicWriteAccessors())
            {
                var members = new LinkedList<MemberInfo>();

                if (MapDestinationPropertyToSource(members, sourceTypeInfo, destProperty.Name, options))
                {
                    var resolvers = members.Select(mi => mi.ToMemberGetter());
                    var destPropertyAccessor = destProperty.ToMemberAccessor();
#if !SILVERLIGHT
                    typeMap.AddPropertyMap(destPropertyAccessor, resolvers);
#else
                    typeMap.AddPropertyMap(destPropertyAccessor, resolvers.Cast<IValueResolver>());
#endif
                }
            }
            if (!destinationType.IsAbstract && destinationType.IsClass)
            {
                foreach (var destCtor in destTypeInfo.GetConstructors().OrderByDescending(ci => ci.GetParameters().Length))
                {
                    if (MapDestinationCtorToSource(typeMap, destCtor, sourceTypeInfo, options))
                    {
                        break;
                    }
                }
            }
            return typeMap;
        }
예제 #2
0
        private bool MapDestinationCtorToSource(TypeMap typeMap, ConstructorInfo destCtor, TypeInfo sourceTypeInfo,
                                                IMappingOptions options)
        {
            var parameters = new List<ConstructorParameterMap>();

            foreach (var parameter in destCtor.GetParameters())
            {
                var members = new LinkedList<MemberInfo>();

                if (!MapDestinationPropertyToSource(members, sourceTypeInfo, parameter.Name, options))
                    return false;

                var resolvers = members.Select(mi => mi.ToMemberGetter());

                var param = new ConstructorParameterMap(parameter, resolvers.ToArray());

                parameters.Add(param);
            }

            typeMap.AddConstructorMap(destCtor, parameters);

            return true;
        }
예제 #3
0
		public TypeMapCreatedEventArgs(TypeMap typeMap)
		{
			TypeMap = typeMap;
		}
 public TypeMapConfigErrors(TypeMap typeMap, string[] unmappedPropertyNames)
 {
   TypeMap = typeMap;
   UnmappedPropertyNames = unmappedPropertyNames;
 }
예제 #5
0
파일: TypeMap.cs 프로젝트: sclcwwl/Gimela
 public void InheritTypes(TypeMap inheritedTypeMap)
 {
     foreach (var includedDerivedType in inheritedTypeMap._includedDerivedTypes
         .Where(includedDerivedType => !_includedDerivedTypes.Contains(includedDerivedType)))
     {
         _includedDerivedTypes.Add(includedDerivedType);
     }
 }
예제 #6
0
파일: TypeMap.cs 프로젝트: sclcwwl/Gimela
 public bool Equals(TypeMap other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return Equals(other._sourceType, _sourceType) && Equals(other._destinationType, _destinationType);
 }
예제 #7
0
        private void DryRunTypeMap(ICollection <TypeMap> typeMapsChecked, ResolutionContext context)
        {
            if (context.TypeMap != null)
            {
                typeMapsChecked.Add(context.TypeMap);
            }

            var mapperToUse = GetMappers().FirstOrDefault(mapper => mapper.IsMatch(context));

            if (mapperToUse == null && context.SourceType.IsNullableType())
            {
                var nullableContext = context.CreateValueContext(null, Nullable.GetUnderlyingType(context.SourceType));

                mapperToUse = GetMappers().FirstOrDefault(mapper => mapper.IsMatch(nullableContext));
            }

            if (mapperToUse == null)
            {
                throw new MapperConfigurationException(context);
            }

            if (mapperToUse is TypeMapMapper)
            {
                foreach (var propertyMap in context.TypeMap.GetPropertyMaps())
                {
                    if (!propertyMap.IsIgnored())
                    {
                        var lastResolver = propertyMap.GetSourceValueResolvers().OfType <IMemberResolver>().LastOrDefault();

                        if (lastResolver != null)
                        {
                            var sourceType      = lastResolver.MemberType;
                            var destinationType = propertyMap.DestinationProperty.MemberType;
                            var memberTypeMap   = ((IConfigurationProvider)this).FindTypeMapFor(null, sourceType, destinationType);

                            if (typeMapsChecked.Any(typeMap => Equals(typeMap, memberTypeMap)))
                            {
                                continue;
                            }

                            var memberContext = context.CreateMemberContext(memberTypeMap, null, null, sourceType, propertyMap);

                            DryRunTypeMap(typeMapsChecked, memberContext);
                        }
                    }
                }
            }
            else if (mapperToUse is ArrayMapper || mapperToUse is EnumerableMapper || mapperToUse is CollectionMapper)
            {
                Type    sourceElementType = TypeHelper.GetElementType(context.SourceType);
                Type    destElementType   = TypeHelper.GetElementType(context.DestinationType);
                TypeMap itemTypeMap       = ((IConfigurationProvider)this).FindTypeMapFor(null, sourceElementType, destElementType);

                if (typeMapsChecked.Any(typeMap => Equals(typeMap, itemTypeMap)))
                {
                    return;
                }

                var memberContext = context.CreateElementContext(itemTypeMap, null, sourceElementType, destElementType, 0);

                DryRunTypeMap(typeMapsChecked, memberContext);
            }
        }
예제 #8
0
        private IMappingExpression <TSource, TDestination> CreateMappingExpression <TSource, TDestination>(TypeMap typeMap)
        {
            IMappingExpression <TSource, TDestination> mappingExp =
                new MappingExpression <TSource, TDestination>(typeMap, _serviceCtor, this);
            // Custom Hack
            var destInfo = new TypeInfo(typeof(TDestination));

            foreach (var destProperty in destInfo.GetPublicWriteAccessors())
            {
                object[] attrs = destProperty.GetCustomAttributes(true);
                if (attrs.Any(x => x is IgnoreMapAttribute))
                {
                    mappingExp = mappingExp.ForMember(destProperty.Name, y => y.Ignore());
                }
            }

            return(mappingExp);
        }
예제 #9
0
 public void AssertConfigurationIsValid(TypeMap typeMap)
 {
     AssertConfigurationIsValid(Enumerable.Repeat(typeMap, 1));
 }
예제 #10
0
        public IMappingExpression <TSource, TDestination> CreateMap <TSource, TDestination>(string profileName, MemberList memberList)
        {
            TypeMap typeMap = CreateTypeMap(typeof(TSource), typeof(TDestination), profileName, memberList);

            return(CreateMappingExpression <TSource, TDestination>(typeMap));
        }