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; }
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; }
public TypeMapCreatedEventArgs(TypeMap typeMap) { TypeMap = typeMap; }
public TypeMapConfigErrors(TypeMap typeMap, string[] unmappedPropertyNames) { TypeMap = typeMap; UnmappedPropertyNames = unmappedPropertyNames; }
public void InheritTypes(TypeMap inheritedTypeMap) { foreach (var includedDerivedType in inheritedTypeMap._includedDerivedTypes .Where(includedDerivedType => !_includedDerivedTypes.Contains(includedDerivedType))) { _includedDerivedTypes.Add(includedDerivedType); } }
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); }
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); } }
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); }
public void AssertConfigurationIsValid(TypeMap typeMap) { AssertConfigurationIsValid(Enumerable.Repeat(typeMap, 1)); }
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)); }