private void MapDestinationCtorToSource(TypeMap typeMap, List <ICtorParameterConfiguration> ctorParamConfigurations)
        {
            var ctorMap = typeMap.ConstructorMap;

            if (ctorMap != null)
            {
                foreach (var paramMap in ctorMap.CtorParams)
                {
                    paramMap.CanResolveValue = paramMap.CanResolveValue || IsConfigured(paramMap.Parameter);
                }
                return;
            }
            if (typeMap.DestinationType.IsAbstract || !typeMap.Profile.ConstructorMappingEnabled)
            {
                return;
            }
            foreach (var destCtor in typeMap.DestinationTypeDetails.Constructors.OrderByDescending(ci => ci.GetParameters().Length))
            {
                var ctorParameters = destCtor.GetParameters();
                if (ctorParameters.Length == 0)
                {
                    break;
                }
                ctorMap = new ConstructorMap(destCtor, typeMap);
                foreach (var parameter in ctorParameters)
                {
                    var resolvers  = new LinkedList <MemberInfo>();
                    var canResolve = typeMap.Profile.MapDestinationPropertyToSource(typeMap.SourceTypeDetails, destCtor.DeclaringType, parameter.GetType(), parameter.Name, resolvers, IsReverseMap);
                    if ((!canResolve && parameter.IsOptional) || IsConfigured(parameter))
                    {
                        canResolve = true;
                    }
                    ctorMap.AddParameter(parameter, resolvers.ToArray(), canResolve);
                }
                typeMap.ConstructorMap = ctorMap;
                if (ctorMap.CanResolve)
                {
                    break;
                }
            }
            return;

            bool IsConfigured(ParameterInfo parameter) => ctorParamConfigurations.Any(c => c.CtorParamName == parameter.Name);
        }
Beispiel #2
0
        private void MapDestinationCtorToSource(TypeMap typeMap)
        {
            var sourceMembers = new List <MemberInfo>();

            foreach (var destCtor in typeMap.DestinationConstructors)
            {
                var  constructor   = destCtor.Constructor;
                var  ctorMap       = new ConstructorMap(constructor, typeMap);
                bool canMapResolve = true;
                foreach (var parameter in destCtor.Parameters)
                {
                    sourceMembers.Clear();
                    var canResolve = typeMap.Profile.MapDestinationPropertyToSource(typeMap.SourceTypeDetails, constructor.DeclaringType, parameter.ParameterType, parameter.Name, sourceMembers, IsReverseMap);
                    if (!canResolve)
                    {
                        if (parameter.IsOptional || IsConfigured(parameter))
                        {
                            canResolve = true;
                        }
                        else
                        {
                            canMapResolve = false;
                        }
                    }
                    ctorMap.AddParameter(parameter, sourceMembers, canResolve);
                }
                typeMap.ConstructorMap = ctorMap;
                if (canMapResolve)
                {
                    ctorMap.CanResolve = true;
                    break;
                }
            }
            return;

            bool IsConfigured(ParameterInfo parameter) => _ctorParamConfigurations?.Any(c => c.CtorParamName == parameter.Name) is true;
        }