Пример #1
0
        public override MapResult Map <T>(ValueMapperContext context)
        {
            try
            {
                var type   = context.Type.ElementType ?? context.Type;
                var result = context.Values.Select(x => (T)(x is string
                                                            ?x.ToString().ParseSimpleType(type) : x));

                var value = context.Type.IsArray
                    ? result.ToArray()
                    : (context.Type.IsGenericListCastable
                        ? (object)result.ToList()
                        : result.FirstOrDefault());

                return(MapResult.Success(value));
            }
            catch (FormatException exception)
            {
                var message = $"{context.Values.Select(x => $"'{x}'").Join(",")} " +
                              $"is not formatted correctly. {exception.Message}";

                return(MapResult.Failure(context.Parameter != null
                    ? $"Parameter {context.Parameter.Name} value {message}"
                    : $"Value {message}"));
            }
        }
Пример #2
0
        public static MapResult Map(this IEnumerable <IValueMapper> mappers,
                                    object[] values, PropertyDescriptor property, RequestContext requestContext,
                                    Configuration configuration)
        {
            var mapperContext = new ValueMapperContext(configuration, requestContext, property, values);
            var mapper        = configuration.ValueMappers.ThatApplyTo(
                mappers, mapperContext).FirstOrDefault();

            return(mapper == null?MapResult.NotMapped() : MapResult.WasMapped(mapper.Map(mapperContext)));
        }
Пример #3
0
        public static MapResult Map(this IEnumerable <IValueMapper> mappers,
                                    ActionMethod actionMethod, RouteDescriptor route,
                                    ActionParameter parameter, object[] values,
                                    Configuration configuration, HttpConfiguration httpConfiguration)
        {
            var mapperContext = new ValueMapperContext(parameter, values);
            var mapperConfigurationcontext = new ValueMapperConfigurationContext(
                configuration, httpConfiguration, actionMethod, route, parameter, values);
            var mapper = configuration.ValueMappers
                         .FirstThatAppliesToOrDefault(mappers, mapperConfigurationcontext, mapperContext);

            return(mapper == null
                ? MapResult.NoMapper()
                : mapper.Map(mapperContext));
        }