public AutoMapperMappingException(ResolutionContext context, Exception inner, PropertyMap propertyMap)
     : base(null, inner)
 {
     Context = context;
     Types = context.Types;
     PropertyMap = propertyMap;
 }
Beispiel #2
0
        public object ResolveValue(ResolutionContext context)
        {
            var ctorArgs = new List<object>();

            foreach (var map in CtorParams)
            {
                var result = map.ResolveValue(context);

                var sourceType = result.Type;
                var destinationType = map.Parameter.ParameterType;

                var typeMap = context.ConfigurationProvider.ResolveTypeMap(result, destinationType);

                Type targetSourceType = typeMap != null ? typeMap.SourceType : sourceType;

                var newContext = context.CreateTypeContext(typeMap, result.Value, null, targetSourceType,
                    destinationType);

                if (typeMap == null && map.Parameter.IsOptional)
                {
                    object value = map.Parameter.DefaultValue;
                    ctorArgs.Add(value);
                }
                else
                {
                    var value = context.Engine.Map(newContext);
                    ctorArgs.Add(value);
                }
            }

            return _runtimeCtor.Value(ctorArgs.ToArray());
        }
        private static bool CheckPropertyMapSkipList(ResolutionContext context, Type formatterType)
        {
            if (context.PropertyMap == null)
                return true;

            return !context.PropertyMap.FormattersToSkipContains(formatterType);
        }
 private ResolutionResult(object value, ResolutionContext context)
 {
     _value = value;
     _context = context;
     _type = ResolveType(value, typeof(object));
     _memberType = _type;
 }
 private void DryRunTypeMap(ICollection<TypeMap> typeMapsChecked, TypePair types, TypeMap typeMap, ResolutionContext context)
 {
     if (typeMap != null)
     {
         typeMapsChecked.Add(typeMap);
         if(typeMap.CustomMapper != null || typeMap.TypeConverterType != null)
         {
             return;
         }
         CheckPropertyMaps(typeMapsChecked, typeMap, context);
     }
     else
     {
         var mapperToUse = _config.GetMappers().FirstOrDefault(mapper => mapper.IsMatch(types));
         if (mapperToUse == null && types.SourceType.IsNullableType())
         {
             var nullableTypes = new TypePair(Nullable.GetUnderlyingType(types.SourceType),
                 types.DestinationType);
             mapperToUse = _config.GetMappers().FirstOrDefault(mapper => mapper.IsMatch(nullableTypes));
         }
         if (mapperToUse == null)
         {
             throw new AutoMapperConfigurationException(types);
         }
         if (mapperToUse is ArrayMapper || mapperToUse is EnumerableMapper || mapperToUse is CollectionMapper)
         {
             CheckElementMaps(typeMapsChecked, types, context);
         }
     }
 }
 private void DryRunTypeMap(ICollection<TypeMap> typeMapsChecked, ResolutionContext context)
 {
     var typeMap = context.TypeMap;
     if (typeMap != null)
     {
         typeMapsChecked.Add(typeMap);
         CheckPropertyMaps(typeMapsChecked, context);
     }
     else
     {
         var mapperToUse = _config.GetMappers().FirstOrDefault(mapper => mapper.IsMatch(context.Types));
         if (mapperToUse == null && context.SourceType.IsNullableType())
         {
             var nullableTypes = new TypePair(Nullable.GetUnderlyingType(context.SourceType),
                 context.DestinationType);
             mapperToUse = _config.GetMappers().FirstOrDefault(mapper => mapper.IsMatch(nullableTypes));
         }
         if (mapperToUse == null)
         {
             throw new AutoMapperConfigurationException(context);
         }
         if (mapperToUse is ArrayMapper || mapperToUse is EnumerableMapper || mapperToUse is CollectionMapper)
         {
             CheckElementMaps(typeMapsChecked, context);
         }
     }
 }
 private ResolutionResult(object value, ResolutionContext context)
 {
     Value = value;
     Context = context;
     Type = ResolveType(value, typeof (object));
     MemberType = Type;
 }
 private ResolutionResult(object value, ResolutionContext context, Type memberType)
 {
     Value = value;
     Context = context;
     Type = ResolveType(value, memberType);
     MemberType = memberType;
 }
 private ResolutionResult(object value, ResolutionContext context, Type memberType)
 {
     _value = value;
     _context = context;
     _type = ResolveType(value, memberType);
     _memberType = memberType;
 }
Beispiel #10
0
        public object CreateObject(ResolutionContext context)
        {
            var typeMap = context.TypeMap;
            var destinationType = context.DestinationType;

            if (typeMap != null)
                if (typeMap.DestinationCtor != null)
                    return typeMap.DestinationCtor(context);
                else if (typeMap.ConstructDestinationUsingServiceLocator)
                    return context.Options.ServiceCtor(destinationType);
                else if (typeMap.ConstructorMap != null && typeMap.ConstructorMap.CtorParams.All(p => p.CanResolve))
                    return typeMap.ConstructorMap.ResolveValue(context);

            if (context.DestinationValue != null)
                return context.DestinationValue;

            if (destinationType.IsInterface())
#if PORTABLE
                throw new PlatformNotSupportedException("Mapping to interfaces through proxies not supported.");
#else
                destinationType = new ProxyGenerator().GetProxyType(destinationType);
#endif

                return !ConfigurationProvider.AllowNullDestinationValues
                ? ObjectCreator.CreateNonNullValue(destinationType)
                : ObjectCreator.CreateObject(destinationType);
        }
        public string FormatValue(ResolutionContext context)
        {
            var timespan = (TimeSpan) context.SourceValue;
            var list = new List<string>(3);

            int days = timespan.Days;
            int hours = timespan.Hours;
            int minutes = timespan.Minutes;

            if (days > 1)
                list.Add(string.Format("{0} days", days));
            else if (days == 1)
                list.Add(string.Format("{0} day", days));

            if (hours > 1)
                list.Add(string.Format("{0} hours", hours));
            else if (hours == 1)
                list.Add(string.Format("{0} hour", hours));

            if (minutes > 1)
                list.Add(string.Format("{0} minutes", minutes));
            else if (minutes == 1)
                list.Add(string.Format("{0} minute", minutes));

            return string.Join(", ", list.ToArray());
        }
 protected object MapAbstractModel(ResolutionContext context)
 {
     var assemblyQualifiedName = context.DestinationType.AssemblyQualifiedName;
     var baseFullName = context.DestinationType.FullName;
     var childtype = ((AbstractForm)context.SourceValue).BindingType;
     var destinationType = Type.GetType(assemblyQualifiedName.Replace(baseFullName, baseFullName + childtype));
     return Mapper.Map(context.SourceValue, context.SourceType, destinationType);
 }
        private ResolutionResult(object value, ResolutionContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            _value = value;
            _context = context;
            _type = ResolveType(value, typeof(object));
            _memberType = _type;
        }
Beispiel #14
0
        /// <summary>
        /// Determines whether the specified context is match.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns><c>true</c> if the specified context is match; otherwise, <c>false</c>.</returns>
        public bool IsMatch( ResolutionContext context )
        {
            if (Mapper.GetAllTypeMaps().Count(m => m.SourceType == context.SourceType && m.DestinationType == context.DestinationType) == 0)
            {
                return true;
            }

            return false;
        }
            public void ReturnsIsFalseWhenSourceTypeIsNotNameValueCollection()
            {
                var rc = new ResolutionContext(null, null, null, typeof(Object), typeof(NameValueCollection), null);
                var nvcm = new NameValueCollectionMapper();

                var result = nvcm.IsMatch(rc);

                result.ShouldBeFalse();
            }
            public void ReturnsTrueWhenBothSourceAndDestinationTypesAreNameValueCollection()
            {
                var rc = new ResolutionContext(null, null, null, typeof(NameValueCollection), typeof(NameValueCollection), null);
                var nvcm = new NameValueCollectionMapper();

                var result = nvcm.IsMatch(rc);

                result.ShouldBeTrue();
            }
            public void ReturnsIsFalseWhenDestinationTypeIsNotNameValueCollection()
            {
                var rc = new ResolutionContext(null, null, null, typeof(NameValueCollection), typeof(Object), null);
                var nvcm = new NameValueCollectionMapper();

                var result = nvcm.IsMatch(rc);

                Assert.IsFalse(result);
            }
 public string FormatValue(ResolutionContext context)
 {
     if (context.SourceValue is decimal)
     {
         var money = (decimal)context.SourceValue;
         return money.FormatMoney();
     }
     return context.SourceValue.ToString();
 }
        public ResolutionResult(ResolutionContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            _value = context.SourceValue;
            _context = context;
            _type = ResolveType(_value, typeof(object));
            _memberType = _type;
        }
 public string FormatValue(ResolutionContext context)
 {
     var sourceType = context.SourceType;
     if(sourceType != typeof(DateTime) && sourceType != typeof(DateTime?)) {
         throw new ArgumentException(sourceType.GetType().FullName + " is not a System.DateTime.", "context");
     }
     var dateTime = context.SourceValue as DateTime?;
     return dateTime.HasValue ? dateTime.Value.ToLongDateString() : "";
 }
            public void ReturnsNullIfSourceValueIsNull()
            {
                var rc = new ResolutionContext(null, null, new NameValueCollection(), typeof(NameValueCollection), typeof(NameValueCollection), null, Mapper.Engine);
                var nvcm = new NameValueCollectionMapper();

                var result = nvcm.Map(rc, null);

                result.ShouldBeNull();
            }
Beispiel #22
0
        /// <summary>
        /// Resolves the value.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public object ResolveValue(ResolutionContext context)
        {
            var ctorArgs = CtorParams
                        .Select(p => p.ResolveValue(context))
                        .Select(result => result.Value)
                        .ToArray();

            return _runtimeCtor(ctorArgs);
        }
Beispiel #23
0
        public string FormatValue(ResolutionContext context)
        {
            if (context.SourceValue == null)
                return null;

            if (!(context.SourceValue is bool))
                return context.SourceValue.ToNullSafeString();

            return bool.Parse(context.SourceValue.ToNullSafeString()) ? "Si" : "No";
        }
        private ResolutionResult(object value, ResolutionContext context, Type memberType)
        {
            if(context == null) throw new ArgumentNullException("context");
            if(memberType == null) throw new ArgumentNullException("memberType");

            _value = value;
            _context = context;
            _type = ResolveType(value, memberType);
            _memberType = memberType;
        }
		public string FormatValue(ResolutionContext context)
		{
			if (context.SourceValue == null)
				return null;

			if (!(context.SourceValue is DateTime))
				return context.SourceValue.ToNullSafeString();

			return ((DateTime) context.SourceValue).ToString("HH:mm");
		}
		public string FormatValue(ResolutionContext context)
		{
			if (context.SourceValue == null)
				return null;

			if (!(context.SourceValue is decimal))
				return context.SourceValue.ToNullSafeString();

			return ((decimal) context.SourceValue).ToString("c");
		}
        public string FormatValue(ResolutionContext context)
        {
            if (context.SourceValue == null)
                return null;

            if (!(context.SourceValue is bool))
                return context.SourceValue.ToNullSafeString();

            return ((bool) context.SourceValue) ? "Yes" : "No";
        }
 public string FormatValue(ResolutionContext context)
 {
     var items = context.DestinationValue as IList<SearchingFor>;
     if (items != null) {
         var list = new List<string>(items.Count);
         list.AddRange(items.Select(item => item.Search.ToString(CultureInfo.InvariantCulture)));
         return string.Join("", list);
     }
     return "";
 }
            public void ReturnsEmptyCollectionWhenSourceCollectionIsEmpty()
            {
                var sourceValue = new NameValueCollection();
                var rc = new ResolutionContext(null, sourceValue, new NameValueCollection(), typeof(NameValueCollection), typeof(NameValueCollection), null);
                var nvcm = new NameValueCollectionMapper();

                var result = nvcm.Map(rc, null) as NameValueCollection;

                result.ShouldBeEmpty();
            }
        private static bool IsCachedMapperFor(ResolutionContext resolutionContext)
        {
            var contextTypePair = CreateTypePair(resolutionContext.SourceType, resolutionContext.DestinationType);

            if (Mapper.Engine._objectMapperCache()[contextTypePair] != null)
            {
                return true;
            }
            return false;
        }
 public AutoMapperMappingException(ResolutionContext context, Exception inner)
     : base(null, inner)
 {
     Context = context;
 }
 public AutoMapperMappingException(ResolutionContext context, string message)
     : this(context)
 {
     _message = message;
 }
Beispiel #33
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 AutoMapperConfigurationException(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);
            }
        }
        public ResolutionResult ResolveValue(ResolutionContext context)
        {
            var result = new ResolutionResult(context);

            return(SourceResolvers.Aggregate(result, (current, resolver) => resolver.Resolve(current)));
        }
Beispiel #35
0
 public IEnumerable <IValueFormatter> GetFormattersToApply(ResolutionContext context)
 {
     return(GetFormatters(context));
 }
Beispiel #36
0
        TDestination IRuntimeMapper.Map <TSource, TDestination>(TSource source, TDestination destination, ResolutionContext context)
        {
            var types = TypePair.Create(source, destination, typeof(TSource), typeof(TDestination));

            var func = _configurationProvider.GetMapperFunc <TSource, TDestination>(types);

            return(func(source, destination, context));
        }
Beispiel #37
0
 object IRuntimeMapper.Map(object source, object destination, Type sourceType, Type destinationType, ResolutionContext context,
                           IMemberMap memberMap)
 => _inner.Map(source, destination, sourceType, destinationType, context, memberMap);
Beispiel #38
0
 TDestination IRuntimeMapper.Map <TSource, TDestination>(TSource source, TDestination destination, ResolutionContext context,
                                                         IMemberMap memberMap)
 => _inner.Map(source, destination, context, memberMap);
Beispiel #39
0
 public MG.FollowupFlag Resolve(UpdatableMessage source, MG.Message destination, MG.FollowupFlag destMember, AM.ResolutionContext context)
 {
     if (source.IsFlagged != null)
     {
         if (destMember == null)
         {
             destMember = new MG.FollowupFlag();
         }
         destMember.FlagStatus = context.Mapper.Map <MG.FollowupFlagStatus?>(source.IsFlagged);
     }
     return(destMember);
 }
 public AutoMapperMappingException(ResolutionContext context)
 {
     Context = context;
 }
Beispiel #41
0
 private static Type GetFormatterType(IValueFormatter formatter, ResolutionContext context)
 {
     return(formatter is DeferredInstantiatedFormatter ? ((DeferredInstantiatedFormatter)formatter).GetFormatterType(context) : formatter.GetType());
 }
Beispiel #42
0
        object IRuntimeMapper.Map(object source, object destination, Type sourceType, Type destinationType, ResolutionContext context)
        {
            var types = TypePair.Create(source, destination, sourceType, destinationType);

            var func = _configurationProvider.GetUntypedMapperFunc(new MapRequest(new TypePair(sourceType, destinationType), types));

            return(func(source, destination, context));
        }
Beispiel #43
0
 public bool ShouldAssignValue(ResolutionContext context)
 {
     return(_condition == null || _condition(context));
 }
Beispiel #44
0
 public Mapper(IConfigurationProvider configurationProvider, Func <Type, object> serviceCtor)
 {
     _configurationProvider = configurationProvider;
     _serviceCtor           = serviceCtor;
     _defaultContext        = new ResolutionContext(new MappingOperationOptions(_serviceCtor), this);
 }
Beispiel #45
0
 public ResolutionContextWrapper(AM.ResolutionContext context)
 {
     this.context = context;
 }
Beispiel #46
0
 public ResolutionContext(object source, object destination, TypePair types, ResolutionContext parent) : this(parent)
 {
     SourceValue      = source;
     DestinationValue = destination;
     Types            = types;
 }
 public ResolutionResult(ResolutionContext context)
     : this(context.SourceValue, context, context.SourceType)
 {
 }
 public static object MapMember(this ResolutionContext context, MemberInfo member, object value)
 => ReflectionHelper.MapMember(context, member, value);