示例#1
0
        public static ParseStringDelegate GetParseMethod(Type type)
        {
            var mapInterface = type.GetTypeWithGenericInterfaceOf(typeof(IDictionary <,>));

            if (mapInterface == null)
            {
                throw new ArgumentException(string.Format("Type {0} is not of type IDictionary<,>", type.FullName));
            }

            //optimized access for regularly used types
            if (type == typeof(Dictionary <string, string>))
            {
                return(ParseStringDictionary);
            }

            var dictionaryArgs = mapInterface.GetGenericArguments();

            var keyTypeParseMethod = Serializer.GetParseFn(dictionaryArgs[KeyIndex]);

            if (keyTypeParseMethod == null)
            {
                return(null);
            }

            var valueTypeParseMethod = Serializer.GetParseFn(dictionaryArgs[ValueIndex]);

            if (valueTypeParseMethod == null)
            {
                return(null);
            }

            var createMapType = type.HasAnyTypeDefinitionsOf(typeof(Dictionary <,>), typeof(IDictionary <,>))
                                ? null : type;

            return(value => ParseDictionaryType(value, createMapType, dictionaryArgs, keyTypeParseMethod, valueTypeParseMethod));
        }
        public static ParseStringDelegate GetParseMethod(Type type)
        {
            var mapInterface = type.GetTypeWithGenericInterfaceOf(typeof(KeyValuePair <,>));

            var keyValuePairArgs   = mapInterface.GenericTypeArguments();
            var keyTypeParseMethod = Serializer.GetParseFn(keyValuePairArgs[KeyIndex]);

            if (keyTypeParseMethod == null)
            {
                return(null);
            }

            var valueTypeParseMethod = Serializer.GetParseFn(keyValuePairArgs[ValueIndex]);

            if (valueTypeParseMethod == null)
            {
                return(null);
            }

            var createMapType = type.HasAnyTypeDefinitionsOf(typeof(KeyValuePair <,>))
                ? null : type;

            return(value => ParseKeyValuePairType(value, createMapType, keyValuePairArgs, keyTypeParseMethod, valueTypeParseMethod));
        }
		public PropertyMap( Type type, ITypeSerializer serializer )
		{
			if (!type.IsClass)
			{
				throw new ArgumentException(@"Cannot construct a property map for a type that isn't a class.");
			}
				
			var propertyInfos = type.GetProperties();

			_setterMap = new Dictionary<string, SetPropertyDelegate>();
			_map = new Dictionary<string, ParseStringDelegate>();

			foreach (var propertyInfo in propertyInfos)
			{
				_map[propertyInfo.Name] = serializer.GetParseFn(propertyInfo.PropertyType);
				_setterMap[propertyInfo.Name] = ParseUtils.GetSetPropertyMethod(type, propertyInfo);
			}
		}
        public static object ParseTuple(Type tupleType, string value)
        {
            var index = 0;

            Serializer.EatMapStartChar(value, ref index);
            if (JsonTypeSerializer.IsEmptyMap(value, index))
            {
                return(tupleType.CreateInstance());
            }

            var genericArgs = tupleType.GetGenericArguments();
            var argValues   = new object[genericArgs.Length];
            var valueLength = value.Length;

            while (index < valueLength)
            {
                var keyValue = Serializer.EatMapKey(value, ref index);
                Serializer.EatMapKeySeperator(value, ref index);
                var elementValue = Serializer.EatValue(value, ref index);
                if (keyValue == null)
                {
                    continue;
                }

                var keyIndex = keyValue.Substring("Item".Length).ToInt() - 1;
                var parseFn  = Serializer.GetParseFn(genericArgs[keyIndex]);
                argValues[keyIndex] = parseFn(elementValue);

                Serializer.EatItemSeperatorOrMapEndChar(value, ref index);
            }

            var ctor = tupleType.DeclaredConstructors()
                       .First(x => x.GetParameters().Length == genericArgs.Length);

            return(ctor.Invoke(argValues));
        }