示例#1
0
        public static string ReplaceRequestQuery <T>(ConverterCollection converters, string uri, string name, T value)
        {
            var typeCode = Type.GetTypeCode(typeof(T));

            if (typeCode == TypeCode.Object)
            {
                if (value == null)
                {
                    return(uri);
                }
                //TODO: ReplaceRequestQuery
                //foreach (var property in value.GetType().GetProperties())
                //{
                //    object propertyValue = property.GetValue(value);
                //    if (propertyValue == null)
                //    {
                //        continue;
                //    }
                //    if (propertyValue is IEnumerable&&propertyValue)
                //    {

                //    }
                //}
                return(uri);
            }
            else
            {
                return(ReplaceRequestQuery(uri, name, ConvertValue <T, string>(converters, value, true)));
            }
        }
        public Utf8Serializer(ConverterCollection converters = null, ArrayPool <byte> bytePool = null)
            : base(converters, bytePool)
        {
            // Integers
            _converters.SetDefault <sbyte, SByteUtf8Converter>();
            _converters.SetDefault <byte, ByteUtf8Converter>();
            _converters.SetDefault <short, Int16Utf8Converter>();
            _converters.SetDefault <ushort, UInt16Utf8Converter>();
            _converters.SetDefault <int, Int32Utf8Converter>();
            _converters.SetDefault <uint, UInt32Utf8Converter>();
            _converters.SetDefault <long, Int64Utf8Converter>();
            _converters.SetDefault <ulong, UInt64Utf8Converter>();

            // Floats
            _converters.SetDefault <float, SingleUtf8Converter>();
            _converters.SetDefault <double, DoubleUtf8Converter>();
            _converters.SetDefault <decimal, DecimalUtf8Converter>();

            // Dates/TimeSpans
            _converters.SetDefault <DateTime, DateTimeUtf8Converter>();
            _converters.SetDefault <DateTimeOffset, DateTimeOffsetUtf8Converter>();
            _converters.SetDefault <TimeSpan, TimeSpanUtf8Converter>();

            // Strings
            _converters.SetDefault <char, CharUtf8Converter>();
            _converters.SetDefault <string, StringUtf8Converter>();
            _converters.SetDefault <Utf8String, Utf8StringUtf8Converter>();

            // Others
            _converters.SetDefault <bool, BooleanUtf8Converter>();
            _converters.SetDefault <Guid, GuidUtf8Converter>();
        }
示例#3
0
        public FeignOptions()
        {
            Assemblies = new List <Assembly>();
            Converters = new ConverterCollection();
            //Converters.AddConverter(new ClassToStringConverter<string>());
            //Converters.AddConverter(new StructToStringConverter<int>());
            //Converters.AddConverter(new StructToStringConverter<long>());
            Converters.AddConverter(new ObjectStringConverter());
            MediaTypeFormatters = new MediaTypeFormatterCollection();
            MediaTypeFormatters.AddFormatter(new JsonMediaTypeFormatter(this));
            MediaTypeFormatters.AddFormatter(new JsonMediaTypeFormatter(Constants.MediaTypes.TEXT_JSON, this));
            MediaTypeFormatters.AddFormatter(new XmlMediaTypeFormatter());
            MediaTypeFormatters.AddFormatter(new XmlMediaTypeFormatter(Constants.MediaTypes.TEXT_XML));
            MediaTypeFormatters.AddFormatter(new FormUrlEncodedMediaTypeFormatter());
            MediaTypeFormatters.AddFormatter(new MultipartFormDataMediaTypeFormatter());
            FeignClientPipeline = new GlobalFeignClientPipeline();
            Lifetime            = FeignClientLifetime.Transient;
            Types = new List <FeignClientTypeInfo>();
            DiscoverServiceCacheTime = TimeSpan.FromMinutes(10);
            PropertyNamingPolicy     = NamingPolicy.CamelCase;
#if USE_SYSTEM_TEXT_JSON
            JsonProvider = new SystemTextJsonProvider();
#else
            JsonProvider = new NewtonsoftJsonProvider();
#endif
        }
示例#4
0
 public FeignOptions()
 {
     Converters = new ConverterCollection();
     Converters.AddConverter(new ObjectStringConverter());
     Assemblies          = new List <Assembly>();
     Lifetime            = ServiceLifetime.Transient;
     FeignClientPipeline = new GlobalFeignClientPipelineBuilder();
 }
示例#5
0
 public FutureNhsBlockService(IConfiguration config, ConverterCollection converters, IFutureNhsContentService futureNhsContentService, IContentTypeService contentTypeService, IContentService contentService)
 {
     _config     = config;
     _converters = converters;
     _futureNhsContentService = futureNhsContentService;
     _contentTypeService      = contentTypeService;
     _contentService          = contentService;
 }
示例#6
0
 public WumpusEtfSerializer(ConverterCollection converters = null, ArrayPool <byte> bytePool = null)
     : base(converters, bytePool)
 {
     _converters.SetGenericDefault(typeof(EntityOrId <>), typeof(EntityOrIdConverter <>),
                                   (t) => t.GenericTypeArguments[0]);
     _converters.SetDefault <Color, ColorConverter>();
     _converters.SetDefault <Image, ImageConverter>();
     _converters.SetDefault <Snowflake, SnowflakeConverter>();
 }
        public void Add_GoodValues_Succeeds()
        {
            //Arrange
            var converterCollection = new ConverterCollection();

            //Act
            converterCollection.Add<string, string>((string source, TypeMappingContext context) => null);

            //Assert
            Assert.IsNotNull(converterCollection.Get(typeof(string), typeof(string)));
        }
示例#8
0
 public ContentResolver(
     IVariationContextAccessor variationContextAccessor,
     ConverterCollection converters,
     ILogger <ContentResolver> logger,
     IPublishedValueFallback publishedValueFallback)
 {
     _variationContextAccessor = variationContextAccessor;
     _converters             = converters;
     _logger                 = logger;
     _publishedValueFallback = publishedValueFallback;
 }
        public void Get_GoodValues_ReturnsConverter()
        {
            //Arrange
            var converterCollection = new ConverterCollection();
            converterCollection.Add<string, string>((string source, TypeMappingContext context) => null);

            //Act
            var value = converterCollection.Get(typeof(string), typeof(string));

            //Assert
            Assert.IsNotNull(value);
        }
 public FutureNhsContentResolver(
     IVariationContextAccessor variationContextAccessor,
     ConverterCollection converters,
     ILogger <FutureNhsContentResolver> logger,
     IPublishedValueFallback publishedValueFallback, IContentTypeService contentTypeService, IContentService contentService)
 {
     _variationContextAccessor = variationContextAccessor;
     _converters             = converters;
     _logger                 = logger;
     _publishedValueFallback = publishedValueFallback;
     _contentTypeService     = contentTypeService;
     _contentService         = contentService;
 }
示例#11
0
    public static void Main()
    {
        ConverterCollection <string> c = new ConverterCollection <string>(
            delegate(string s) { return(s.ToUpper()); });

        c.Add("Hello");
        c.Add("World!");

        foreach (string s in c)
        {
            Console.WriteLine(s);
        }
    }
示例#12
0
        public static TResult ConvertValue <TSource, TResult>(ConverterCollection converters, TSource value, bool useDefault)
        {
            var converter = converters.FindConverter <TSource, TResult>();

            if (converter == null)
            {
                if (!useDefault)
                {
                    return(default(TResult));
                }
                return(converters.FindConverter <object, TResult>().Convert(value));
            }
            return(converter.Convert(value));
        }
示例#13
0
        public static string ReplaceRequestParam <T>(ConverterCollection converters, string uri, string name, T value)
        {
            if (Type.GetTypeCode(typeof(T)) == TypeCode.Object)
            {
                var converter = converters.FindConverter <T, string>();
                if (converter != null)
                {
                    return(ReplaceRequestParam(uri, name, converter.Convert(value)));
                }

                // get properties

                foreach (var property in typeof(T).GetProperties())
                {
                    if (property.GetMethod == null)
                    {
                        continue;
                    }
                    object propertyValue = property.GetValue(value);
                    if (propertyValue == null)
                    {
                        continue;
                    }
                    if (propertyValue is string)
                    {
                        uri = ReplaceRequestParam(uri, property.Name, propertyValue.ToString());
                        continue;
                    }
                    if (propertyValue is IEnumerable)
                    {
                        foreach (var item in propertyValue as IEnumerable)
                        {
                            if (item == null)
                            {
                                continue;
                            }
                            uri = ReplaceRequestParam(uri, property.Name, converters.ConvertValue <string>(item, true));
                        }
                        continue;
                    }
                    uri = ReplaceRequestParam(uri, property.Name, converters.ConvertValue <string>(propertyValue, true));
                }

                return(uri);
            }

            return(ReplaceRequestParam(uri, name, converters.ConvertValue <T, string>(value, true)));
        }
示例#14
0
 public FeignOptions()
 {
     Assemblies = new List <Assembly>();
     Converters = new ConverterCollection();
     Converters.AddConverter(new ObjectStringConverter());
     MediaTypeFormatters = new MediaTypeFormatterCollection();
     MediaTypeFormatters.AddFormatter(new JsonMediaTypeFormatter());
     MediaTypeFormatters.AddFormatter(new JsonMediaTypeFormatter(Constants.MediaTypes.TEXT_JSON));
     MediaTypeFormatters.AddFormatter(new XmlMediaTypeFormatter());
     MediaTypeFormatters.AddFormatter(new XmlMediaTypeFormatter(Constants.MediaTypes.TEXT_XML));
     MediaTypeFormatters.AddFormatter(new FormUrlEncodedMediaTypeFormatter());
     MediaTypeFormatters.AddFormatter(new MultipartFormDataMediaTypeFormatter());
     FeignClientPipeline = new GlobalFeignClientPipeline();
     Lifetime            = FeignClientLifetime.Transient;
     Types = new List <FeignClientTypeInfo>();
     DiscoverServiceCacheTime = TimeSpan.FromMinutes(10);
 }
 public FeignOptions()
 {
     Assemblies = new List <Assembly>();
     Converters = new ConverterCollection();
     Converters.AddConverter(new ObjectStringConverter());
     MediaTypeFormatters = new MediaTypeFormatterCollection();
     MediaTypeFormatters.AddFormatter(new JsonMediaTypeFormatter());
     MediaTypeFormatters.AddFormatter(new FormUrlEncodedMediaTypeFormatter());
     MediaTypeFormatters.AddFormatter(new MultipartFormDataMediaTypeFormatter());
     MediaTypeFormatters.AddFormatter(new XmlMediaTypeFormatter());
     MediaTypeFormatters.AddFormatter(new XmlMediaTypeFormatter()
     {
         MediaType = "text/xml"
     });
     FeignClientPipeline = new GlobalFeignClientPipeline();
     Lifetime            = FeignClientLifetime.Transient;
 }
示例#16
0
        public static string ReplaceRequestQuery <T>(ConverterCollection converters, NamingPolicy namingPolicy, string uri, string name, T value, bool urlEncode)
        {
            var typeCode = Type.GetTypeCode(typeof(T));

            if (typeCode == TypeCode.Object)
            {
                foreach (var item in GetObjectStringParameters(name, value, converters, namingPolicy))
                {
                    uri = ReplaceRequestQuery(uri, item.Key, item.Value, urlEncode);
                }
                return(uri);
            }
            else
            {
                return(ReplaceRequestQuery(uri, name, converters.ConvertValue <T, string>(value, true), urlEncode));
            }
        }
示例#17
0
 public MapCompiler(ConverterCollection converters, ObjectFactory objectFactory)
 {
     this.converters = converters;
     this.objectFactory = objectFactory;
     getters = new Dictionary<PropertyInfo, Func<object, object>>();
     setters = new Dictionary<PropertyInfo, Action<object, object>>();
     rootCompilers = new Dictionary<Type, Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>>();
     nodeCompilers = new Dictionary<PropertyInfo, Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>>();
     rootCompilers.Add(typeof(TypeMap), new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(TypeMap));
     rootCompilers.Add(typeof(ReversiveTypeMap), new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(ReversiveTypeMap));
     nodeCompilers.Add(ReflectionHelper.GetMemberInfo<TypeMap, Action<object, object, TypeMappingContext>>((TypeMap o) => o.Mapper) as PropertyInfo, new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(MapWithMapper));
     nodeCompilers.Add(ReflectionHelper.GetMemberInfo<TypeMap, ICollection<PropertyMap>>((TypeMap o) => o.PropertyMaps) as PropertyInfo, new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(TypeMapWithPropertyMaps));
     nodeCompilers.Add(ReflectionHelper.GetMemberInfo<PropertyMap, Action<object, object, TypeMappingContext>>((PropertyMap o) => o.Mapper) as PropertyInfo, new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(MapWithMapper));
     nodeCompilers.Add(ReflectionHelper.GetMemberInfo<PropertyMap, ICollection<TypeMap>>((PropertyMap o) => o.InheritanceMaps) as PropertyInfo, new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(PropertyMapWithInheritanceMaps));
     nodeCompilers.Add(ReflectionHelper.GetMemberInfo<ReversiveTypeMap, Action<object, object, TypeMappingContext>>((ReversiveTypeMap o) => o.Mapper) as PropertyInfo, new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(ReversiveTypeMapWithMapper));
     nodeCompilers.Add(ReflectionHelper.GetMemberInfo<ReversiveTypeMap, ICollection<ReversivePropertyMap>>((ReversiveTypeMap o) => o.PropertyMaps) as PropertyInfo, new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(ReversiveTypeMapWithPropertyMaps));
     nodeCompilers.Add(ReflectionHelper.GetMemberInfo<ReversivePropertyMap, Action<object, object, TypeMappingContext>>((ReversivePropertyMap o) => o.Mapper) as PropertyInfo, new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(ReversivePropertyMapWithMapper));
     nodeCompilers.Add(ReflectionHelper.GetMemberInfo<ReversivePropertyMap, ICollection<ReversiveTypeMap>>((ReversivePropertyMap o) => o.InheritanceMaps) as PropertyInfo, new Func<MapCompiler.CompilationContext, MapCompiler.CompilationResult>(ReversivePropertyMapWithInheritanceMaps));
 }
示例#18
0
        public bool TryGetValue(string identifier, out object value)
        {
            IFieldDefinition targetField = ModelDefinition.Fields.FirstOrDefault(f => f.Identifier == identifier);

            if (targetField == null)
            {
                throw Ensure.Exception.ArgumentOutOfRange("identifier", "Unnable to find field '{0}' in model '{1}'.", identifier, ModelDefinition.Identifier);
            }

            string sourceValue;

            if (Storage.TryGetValue(identifier, out sourceValue))
            {
                if (ConverterCollection.TryConvert(sourceValue, targetField, out value))
                {
                    return(true);
                }
            }

            value = null;
            return(false);
        }
示例#19
0
        public static string ReplaceRequestQuery <T>(ConverterCollection converters, string uri, string name, T value)
        {
            var typeCode = Type.GetTypeCode(typeof(T));

            if (typeCode == TypeCode.Object)
            {
                if (value == null)
                {
                    return(uri);
                }
                //Nullable<>
                if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    return(ReplaceRequestQuery(uri, name, converters.ConvertValue <T, string>(value, true)));
                }

                if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
                {
                    foreach (var item in value as IEnumerable)
                    {
                        if (item == null)
                        {
                            continue;
                        }
                        uri = ReplaceRequestQuery(uri, name, converters.ConvertValue <string>(item, true));
                    }
                    return(uri);
                }

                //TODO: ReplaceRequestQuery
                //foreach (var property in value.GetType().GetProperties())
                //{
                //    object propertyValue = property.GetValue(value);
                //    if (propertyValue == null)
                //    {
                //        continue;
                //    }
                //    if (propertyValue is IEnumerable&&propertyValue)
                //    {

                //    }
                //}

                // get properties

                foreach (var property in typeof(T).GetProperties())
                {
                    if (property.GetMethod == null)
                    {
                        continue;
                    }
                    object propertyValue = property.GetValue(value);
                    if (propertyValue == null)
                    {
                        continue;
                    }
                    if (propertyValue is string)
                    {
                        uri = ReplaceRequestQuery(uri, property.Name, propertyValue.ToString());
                        continue;
                    }
                    if (propertyValue is IEnumerable)
                    {
                        foreach (var item in propertyValue as IEnumerable)
                        {
                            if (item == null)
                            {
                                continue;
                            }
                            uri = ReplaceRequestQuery(uri, property.Name, converters.ConvertValue <string>(item, true));
                        }
                        continue;
                    }
                    uri = ReplaceRequestQuery(uri, property.Name, converters.ConvertValue <string>(propertyValue, true));
                }

                return(uri);
            }
            else
            {
                return(ReplaceRequestQuery(uri, name, converters.ConvertValue <T, string>(value, true)));
            }
        }
 public VoltaicJsonOutputFormatter(ConverterCollection converters = null, ArrayPool <byte> pool = null)
     : this(new JsonSerializer(converters, pool), pool)
 {
 }
示例#21
0
 public TwitchJsonSerializer(ConverterCollection converters = null, ArrayPool <byte> bytePool = null)
     : base(converters, bytePool)
 {
     _converters.SetDefault <DateTime, DateTimeConverter>();
 }
        public EtfSerializer(ConverterCollection converters = null, ArrayPool <byte> bytePool = null)
            : base(converters, bytePool)
        {
            // Integers
            _converters.SetDefault <sbyte, SByteEtfConverter>(
                (t, p) => new SByteEtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <byte, ByteEtfConverter>(
                (t, p) => new ByteEtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <short, Int16EtfConverter>(
                (t, p) => new Int16EtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <ushort, UInt16EtfConverter>(
                (t, p) => new UInt16EtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <int, Int32EtfConverter>(
                (t, p) => new Int32EtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <uint, UInt32EtfConverter>(
                (t, p) => new UInt32EtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <long, Int64EtfConverter>(
                (t, p) => new Int64EtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <ulong, UInt64EtfConverter>(
                (t, p) => new UInt64EtfConverter(GetStandardFormat(p)));

            // Floats
            _converters.SetDefault <float, SingleEtfConverter>(
                (t, p) => new SingleEtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <double, DoubleEtfConverter>(
                (t, p) => new DoubleEtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <decimal, DecimalEtfConverter>(
                (t, p) => new DecimalEtfConverter(GetStandardFormat(p)));

            // Dates/TimeSpans
            _converters.SetDefault <DateTime, DateTimeEtfConverter>(
                (t, p) => new DateTimeEtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <DateTimeOffset, DateTimeOffsetEtfConverter>(
                (t, p) => new DateTimeOffsetEtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <TimeSpan, TimeSpanEtfConverter>(
                (t, p) => new TimeSpanEtfConverter(GetStandardFormat(p)));
            _converters.AddConditional <DateTime, DateTimeEpochConverter>(
                (t, p) => p?.GetCustomAttribute <EpochAttribute>() != null,
                (t, p) => new DateTimeEpochConverter(this, p.GetCustomAttribute <EpochAttribute>().Type));
            _converters.AddConditional <DateTimeOffset, DateTimeOffsetEpochConverter>(
                (t, p) => p?.GetCustomAttribute <EpochAttribute>() != null,
                (t, p) => new DateTimeOffsetEpochConverter(this, p.GetCustomAttribute <EpochAttribute>().Type));
            _converters.AddConditional <TimeSpan, TimeSpanEpochConverter>(
                (t, p) => p?.GetCustomAttribute <EpochAttribute>() != null,
                (t, p) => new TimeSpanEpochConverter(this, p.GetCustomAttribute <EpochAttribute>().Type));

            // Collections
            _converters.AddGlobalConditional(typeof(ArrayEtfConverter <>),
                                             (t, p) => t.IsArray,
                                             (t) => t.GetElementType());
            _converters.SetGenericDefault(typeof(List <>), typeof(ListEtfConverter <>),
                                          (t) => t.GenericTypeArguments[0]);
            _converters.AddGenericConditional(typeof(Dictionary <,>), typeof(DictionaryEtfConverter <>),
                                              (t, p) => t.GenericTypeArguments[0] == typeof(string),
                                              (t) => t.GenericTypeArguments[1]);

            // Strings
            _converters.SetDefault <char, CharEtfConverter>();
            _converters.SetDefault <string, StringEtfConverter>();
            _converters.SetDefault <Utf8String, Utf8StringEtfConverter>();

            // Enums
            _converters.AddGlobalConditional(typeof(Int64EnumEtfConverter <>),
                                             (t, p) => t.IsEnum && (
                                                 Enum.GetUnderlyingType(t.AsType()) == typeof(sbyte) ||
                                                 Enum.GetUnderlyingType(t.AsType()) == typeof(short) ||
                                                 Enum.GetUnderlyingType(t.AsType()) == typeof(int) ||
                                                 Enum.GetUnderlyingType(t.AsType()) == typeof(long)),
                                             (t) => t.AsType());
            _converters.AddGlobalConditional(typeof(UInt64EnumEtfConverter <>),
                                             (t, p) => t.IsEnum && (
                                                 Enum.GetUnderlyingType(t.AsType()) == typeof(byte) ||
                                                 Enum.GetUnderlyingType(t.AsType()) == typeof(ushort) ||
                                                 Enum.GetUnderlyingType(t.AsType()) == typeof(uint) ||
                                                 Enum.GetUnderlyingType(t.AsType()) == typeof(ulong)),
                                             (t) => t.AsType());

            // Others
            _converters.SetDefault <bool, BooleanEtfConverter>(
                (t, p) => new BooleanEtfConverter(GetStandardFormat(p)));
            _converters.SetDefault <Guid, GuidEtfConverter>(
                (t, p) => new GuidEtfConverter(GetStandardFormat(p)));
            _converters.SetGenericDefault(typeof(Nullable <>), typeof(NullableEtfConverter <>),
                                          (t) => t.GenericTypeArguments[0]);
            _converters.SetGenericDefault(typeof(Optional <>), typeof(OptionalEtfConverter <>),
                                          (t) => t.GenericTypeArguments[0]);
            _converters.AddGlobalConditional(typeof(ObjectEtfConverter <>),
                                             (t, p) => t.IsClass,
                                             (t) => t.AsType());
        }
示例#23
0
 public PubsubJsonSerializer(ConverterCollection converters = null, ArrayPool <byte> bytePool = null)
     : base(converters, bytePool)
 {
     _converters.SetDefault <Topic, TopicConverter>();
 }
 public static IMvcBuilder AddVoltaicJsonSerializerFormatters(this IMvcBuilder builder, ConverterCollection converters = null, ArrayPool <byte> pool = null)
 => AddVoltaicJsonSerializerFormatters(builder, new JsonSerializer(converters, pool), pool);
示例#25
0
        public static Action<object, object, TypeMappingContext> GetSimpleEnumerablePropertiesMapper(
			PropertyInfo fromPropertyInfo, PropertyInfo toPropertyInfo, ObjectFactory objectFactory,
			ConverterCollection converters, Func<object, object> fromPropertyGetter, Action<object, object> toPropertySetter)
        {
            if (fromPropertyInfo.PropertyType == toPropertyInfo.PropertyType)
            {
                return delegate(object from, object to, TypeMappingContext ctxt)
                {
                    toPropertySetter(to, fromPropertyGetter(from));
                };
            }
            else
            {
                Type enumerableItemType = ReflectionHelper.GetEnumerableItemType(fromPropertyInfo.PropertyType);
                Type toItemType = ReflectionHelper.GetEnumerableItemType(toPropertyInfo.PropertyType);
                var addMethod = ReflectionHelper.GetAddItemMethod(toPropertyInfo.PropertyType);
                var addMethodInfo = ReflectionHelper.GetAddItemMethodInfo(toPropertyInfo.PropertyType);
                var converter = converters.Get(enumerableItemType, toItemType);

                if (converter == null)
                {
                    converter = ((object fromProperty, TypeMappingContext context) =>
                        Convert.ChangeType(fromProperty, toItemType));
                }

                return delegate(object from, object to, TypeMappingContext context)
                {
                    object obj = fromPropertyGetter(from);
                    if (obj != null)
                    {
                        object targetObject = objectFactory.CreateTargetObject(
                            obj, toPropertyInfo.PropertyType, context.MappingContext);

                        int num = 0;

                        foreach (object current in obj as IEnumerable)
                        {
                            addMethod(targetObject, converter(current, context), num, addMethodInfo);
                            num++;
                        }

                        toPropertySetter(to, targetObject);
                    }
                };
            }
        }
示例#26
0
 /// <summary>
 /// Initializes a new instance of the MultiConverter
 /// </summary>
 public MultiConverter()
 {
     this.Converters = new ConverterCollection();
 }
示例#27
0
        public static Action<object, object, TypeMappingContext> GetSimplePropertiesMapper(
			PropertyInfo fromPropertyInfo, PropertyInfo toPropertyInfo, ConverterCollection converters,
			Func<object, object> fromPropertyGetter, Action<object, object> toPropertySetter)
        {
            var converter = converters.Get(fromPropertyInfo.PropertyType, toPropertyInfo.PropertyType);

            if (converter == null)
            {
                converter = ((object fromProperty, TypeMappingContext context) =>
                    Convert.ChangeType(fromProperty, toPropertyInfo.PropertyType));
            }

            return delegate(object from, object to, TypeMappingContext context)
            {
                toPropertySetter(to, converter(fromPropertyGetter(from), context));
            };
        }
示例#28
0
 public static string ReplaceRequestParam <T>(ConverterCollection converters, string uri, string name, T value)
 {
     return(ReplaceRequestParam(uri, name, ConvertValue <T, string>(converters, value, true)));
 }
示例#29
0
        public void ConstructorTest()
        {
#pragma warning disable 618
            var converterCollection = new ConverterCollection<string, string>(new string[] { }, x => "Test");
#pragma warning restore 618
        }
示例#30
0
        public static IEnumerable <KeyValuePair <string, string> > GetObjectStringParameters <T>(string name, T value, ConverterCollection converters, NamingPolicy namingPolicy)
        {
            if (value == null)
            {
                yield break;
            }
            //Nullable<>
            if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                yield return(new KeyValuePair <string, string>(name, converters.ConvertValue <T, string>(value, true)));

                yield break;
            }

            if (typeof(IDictionary).IsAssignableFrom(typeof(T)))
            {
                IDictionary map = ((IDictionary)value);
                foreach (var item in map.Keys)
                {
                    if (map[item] == null)
                    {
                        continue;
                    }
                    yield return(new KeyValuePair <string, string>(item.ToString(), converters.ConvertValue <string>(item, true)));
                }
                yield break;
            }

            if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
            {
                foreach (var item in value as IEnumerable)
                {
                    if (item == null)
                    {
                        continue;
                    }
                    yield return(new KeyValuePair <string, string>(name, converters.ConvertValue <string>(item, true)));
                }
                yield break;
            }

            //TODO: ReplaceRequestQuery
            //foreach (var property in value.GetType().GetProperties())
            //{
            //    object propertyValue = property.GetValue(value);
            //    if (propertyValue == null)
            //    {
            //        continue;
            //    }
            //    if (propertyValue is IEnumerable&&propertyValue)
            //    {

            //    }
            //}

            // get properties

            foreach (var property in typeof(T).GetProperties())
            {
                if (property.GetMethod == null)
                {
                    continue;
                }
                object propertyValue = property.GetValue(value);
                if (propertyValue == null)
                {
                    continue;
                }
                if (propertyValue is string)
                {
                    yield return(new KeyValuePair <string, string>(GetName(property, namingPolicy), propertyValue.ToString()));

                    continue;
                }
                if (propertyValue is IEnumerable)
                {
                    foreach (var item in propertyValue as IEnumerable)
                    {
                        if (item == null)
                        {
                            continue;
                        }
                        yield return(new KeyValuePair <string, string>(GetName(property, namingPolicy), converters.ConvertValue <string>(item, true)));
                    }
                    continue;
                }
                yield return(new KeyValuePair <string, string>(GetName(property, namingPolicy), converters.ConvertValue <string>(propertyValue, true)));
            }
        }
示例#31
0
 public static string ReplacePathVariable <T>(ConverterCollection converters, string uri, string name, T value)
 {
     return(ReplacePathVariable(uri, name, converters.ConvertValue <T, string>(value, true)));
 }