Exemplo n.º 1
0
        private bool MapDestinationCtorToSource(TypeMap typeMap, ConstructorInfo destCtor, TypeDetails sourceTypeInfo, ProfileMap options)
        {
            var ctorParameters = destCtor.GetParameters();

            if (ctorParameters.Length == 0 || !options.ConstructorMappingEnabled)
                return false;

            var ctorMap = new ConstructorMap(destCtor, typeMap);

            foreach (var parameter in ctorParameters)
            {
                var resolvers = new LinkedList<MemberInfo>();

                var canResolve = MapDestinationPropertyToSource(options, sourceTypeInfo, destCtor.DeclaringType, parameter.GetType(), parameter.Name, resolvers);
                if(!canResolve && parameter.HasDefaultValue)
                {
                    canResolve = true;
                }

                ctorMap.AddParameter(parameter, resolvers.ToArray(), canResolve);
            }

            typeMap.ConstructorMap = ctorMap;

            return true;
        }
Exemplo n.º 2
0
        public TypeMap CreateTypeMap(Type sourceType, Type destinationType, ProfileMap options, MemberList memberList)
        {
            var sourceTypeInfo = options.CreateTypeDetails(sourceType);
            var destTypeInfo = options.CreateTypeDetails(destinationType);

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

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

                if (MapDestinationPropertyToSource(options, sourceTypeInfo, destProperty.DeclaringType, destProperty.GetMemberType(), destProperty.Name, resolvers))
                {
                    typeMap.AddPropertyMap(destProperty, resolvers);
                }
            }
            if (!destinationType.IsAbstract() && destinationType.IsClass())
            {
                foreach (var destCtor in destTypeInfo.Constructors.OrderByDescending(ci => ci.GetParameters().Length))
                {
                    if (MapDestinationCtorToSource(typeMap, destCtor, sourceTypeInfo, options))
                    {
                        break;
                    }
                }
            }
            return typeMap;
        }
Exemplo n.º 3
0
 public TypeMap(TypeDetails sourceType, TypeDetails destinationType, MemberList memberList, ProfileMap profile)
 {
     SourceTypeDetails = sourceType;
     DestinationTypeDetails = destinationType;
     Types = new TypePair(sourceType.Type, destinationType.Type);
     Profile = profile;
     ConfiguredMemberList = memberList;
 }
Exemplo n.º 4
0
 public TypeDetails(Type type, ProfileMap config)
 {
     Type = type;
     var membersToMap = MembersToMap(config.ShouldMapProperty, config.ShouldMapField);
     var publicReadableMembers = GetAllPublicReadableMembers(membersToMap);
     var publicWritableMembers = GetAllPublicWritableMembers(membersToMap);
     PublicReadAccessors = BuildPublicReadAccessors(publicReadableMembers);
     PublicWriteAccessors = BuildPublicAccessors(publicWritableMembers);
     PublicNoArgMethods = BuildPublicNoArgMethods();
     Constructors = type.GetDeclaredConstructors().Where(ci => !ci.IsStatic).ToArray();
     PublicNoArgExtensionMethods = BuildPublicNoArgExtensionMethods(config.SourceExtensionMethods);
     AllMembers = PublicReadAccessors.Concat(PublicNoArgMethods).Concat(PublicNoArgExtensionMethods).ToList();
     DestinationMemberNames = AllMembers.Select(mi => new DestinationMemberName { Member = mi, Possibles = PossibleNames(mi.Name, config.Prefixes, config.Postfixes).ToArray() });
 }
Exemplo n.º 5
0
        private bool MapDestinationCtorToSource(TypeMap typeMap, ConstructorInfo destCtor, TypeDetails sourceTypeInfo, ProfileMap options)
        {
            var ctorParameters = destCtor.GetParameters();

            if (ctorParameters.Length == 0 || !options.ConstructorMappingEnabled)
            {
                return(false);
            }

            var ctorMap = new ConstructorMap(destCtor, typeMap);

            foreach (var parameter in ctorParameters)
            {
                var resolvers = new LinkedList <MemberInfo>();

                var canResolve = MapDestinationPropertyToSource(options, sourceTypeInfo, destCtor.DeclaringType, parameter.GetType(), parameter.Name, resolvers);
                if (!canResolve && parameter.IsOptional)
                {
                    canResolve = true;
                }
                ctorMap.AddParameter(parameter, resolvers.ToArray(), canResolve);
            }

            typeMap.ConstructorMap = ctorMap;

            return(ctorMap.CanResolve);
        }
Exemplo n.º 6
0
 private bool MapDestinationPropertyToSource(ProfileMap options, TypeDetails sourceTypeInfo, Type destType, Type destMemberType, string destMemberInfo, LinkedList <MemberInfo> members)
 {
     return(options.MemberConfigurations.Any(_ => _.MapDestinationPropertyToSource(options, sourceTypeInfo, destType, destMemberType, destMemberInfo, members)));
 }
Exemplo n.º 7
0
 public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
 {
     return(Call(Constant(this), MapMethod, ToType(sourceExpression, typeof(TSource)), ToType(destExpression, typeof(TDestination)), contextExpression));
 }
Exemplo n.º 8
0
 public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap,
                                 IMemberMap memberMap, Expression sourceExpression, Expression destExpression,
                                 Expression contextExpression) =>
 Call(
     Constant(this),
     MapMethod,
     ToType(sourceExpression, typeof(TSource)),
     ToType(destExpression, typeof(TDestination)),
     Constant(sourceExpression.Type),
     Constant(destExpression.Type),
     contextExpression);
Exemplo n.º 9
0
 public TypeMap(TypeDetails sourceType, TypeDetails destinationType, MemberList memberList, ProfileMap profile)
 {
     SourceTypeDetails      = sourceType;
     DestinationTypeDetails = destinationType;
     Types   = new TypePair(sourceType.Type, destinationType.Type);
     Profile = profile;
     ConfiguredMemberList = memberList;
 }
Exemplo n.º 10
0
 private bool MapDestinationPropertyToSource(ProfileMap options, TypeDetails sourceTypeInfo, Type destType, Type destMemberType, string destMemberInfo, LinkedList<MemberInfo> members)
 {
     return options.MemberConfigurations.Any(_ => _.MapDestinationPropertyToSource(options, sourceTypeInfo, destType, destMemberType, destMemberInfo, members));
 }