示例#1
0
            internal override ReflectionUtils.ConstructorDelegate ContructorDelegateFactory(Type key)
            {
                if (!key.IsArray && !key.GetTypeInfo().IsInterface&& ReflectionUtils.IsAssignableFrom(typeof(IList), key))
                {
                    ReflectionUtils.ConstructorDelegate ctor = ReflectionUtils.GetContructor(key, ArrayConstructorParameterTypes);
                    if (ctor != null)
                    {
                        return(ctor);
                    }
#if NET_2_0
                    ctor = base.ContructorDelegateFactory(key);
                    return(args => ctor(new object[0]));
#endif
                }
                return(base.ContructorDelegateFactory(key));
            }
        internal override ReflectionUtils.ConstructorDelegate ContructorDelegateFactory(Type key)
        {
            if (!key.IsInterface)
            {
                return(base.ContructorDelegateFactory(key));
            }

            var factory = messageTypeFactory.GetTypeFactory(key);

            ReflectionUtils.ConstructorDelegate constructorDelegate = delegate
            {
                return(factory());
            };

            return(constructorDelegate);
        }
示例#3
0
        public virtual object DeserializeObject(object value, Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            string str = value as string;

            if ((type == typeof(Guid)) && string.IsNullOrEmpty(str))
            {
                return(default(Guid));
            }
            if (value == null)
            {
                return(null);
            }
            object obj2 = null;

            if (str == null)
            {
                if (value as bool)
                {
                    return(value);
                }
            }
            else
            {
                if (str.Length != 0)
                {
                    Uri uri;
                    return(((type == typeof(DateTime)) || (ReflectionUtils.IsNullableType(type) && (Nullable.GetUnderlyingType(type) == typeof(DateTime)))) ? DateTime.ParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal) : (((type == typeof(DateTimeOffset)) || (ReflectionUtils.IsNullableType(type) && (Nullable.GetUnderlyingType(type) == typeof(DateTimeOffset)))) ? ((object)DateTimeOffset.ParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal)) : (((type == typeof(Guid)) || (ReflectionUtils.IsNullableType(type) && (Nullable.GetUnderlyingType(type) == typeof(Guid)))) ? ((object)new Guid(str)) : (!(type == typeof(Uri)) ? (!(type == typeof(string)) ? Convert.ChangeType(str, type, CultureInfo.InvariantCulture) : str) : ((!Uri.IsWellFormedUriString(str, UriKind.RelativeOrAbsolute) || !Uri.TryCreate(str, UriKind.RelativeOrAbsolute, out uri)) ? null : uri)))));
                }
                if (!(type == typeof(Guid)))
                {
                    obj2 = (!ReflectionUtils.IsNullableType(type) || !(Nullable.GetUnderlyingType(type) == typeof(Guid))) ? str : null;
                }
                else
                {
                    Guid guid = default(Guid);
                    obj2 = guid;
                }
                if (!ReflectionUtils.IsNullableType(type) && (Nullable.GetUnderlyingType(type) == typeof(Guid)))
                {
                    return(str);
                }
            }
            bool flag  = value is long;
            bool flag2 = value is double;

            if ((flag && (type == typeof(long))) || (flag2 && (type == typeof(double))))
            {
                return(value);
            }
            if ((flag2 && (type != typeof(double))) || (flag && (type != typeof(long))))
            {
                obj2 = ((type == typeof(int)) || ((type == typeof(long)) || ((type == typeof(double)) || ((type == typeof(float)) || ((type == typeof(bool)) || ((type == typeof(decimal)) || ((type == typeof(byte)) || (type == typeof(short))))))))) ? Convert.ChangeType(value, type, CultureInfo.InvariantCulture) : value;
                return(!ReflectionUtils.IsNullableType(type) ? obj2 : ReflectionUtils.ToNullableType(obj2, type));
            }
            IDictionary <string, object> dictionary = value as IDictionary <string, object>;

            if (dictionary != null)
            {
                IDictionary <string, object> dictionary2 = dictionary;
                if (ReflectionUtils.IsTypeDictionary(type))
                {
                    Type[]      genericTypeArguments = ReflectionUtils.GetGenericTypeArguments(type);
                    Type        type2         = genericTypeArguments[0];
                    Type        type3         = genericTypeArguments[1];
                    Type[]      typeArguments = new Type[] { type2, type3 };
                    Type        type4         = typeof(Dictionary <,>).MakeGenericType(typeArguments);
                    IDictionary dictionary3   = (IDictionary)this.ConstructorCache[type4](new object[0]);
                    foreach (KeyValuePair <string, object> pair in dictionary2)
                    {
                        dictionary3.Add(pair.Key, this.DeserializeObject(pair.Value, type3));
                    }
                    obj2 = dictionary3;
                }
                else if (type == typeof(object))
                {
                    obj2 = value;
                }
                else
                {
                    obj2 = this.ConstructorCache[type](new object[0]);
                    foreach (KeyValuePair <string, KeyValuePair <Type, ReflectionUtils.SetDelegate> > pair2 in this.SetCache[type])
                    {
                        object obj3;
                        if (dictionary2.TryGetValue(pair2.Key, out obj3))
                        {
                            obj3 = this.DeserializeObject(obj3, pair2.Value.Key);
                            pair2.Value.Value(obj2, obj3);
                        }
                    }
                }
            }
            else
            {
                IList <object> list = value as IList <object>;
                if (list != null)
                {
                    IList <object> list2 = list;
                    IList          list3 = null;
                    if (type.IsArray)
                    {
                        object[] args = new object[] { list2.Count };
                        list3 = (IList)this.ConstructorCache[type](args);
                        int num = 0;
                        foreach (object obj4 in list2)
                        {
                            list3[num++] = this.DeserializeObject(obj4, type.GetElementType());
                        }
                    }
                    else if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || ReflectionUtils.IsAssignableFrom(typeof(IList), type))
                    {
                        Type genericListElementType = ReflectionUtils.GetGenericListElementType(type);
                        ReflectionUtils.ConstructorDelegate local1 = this.ConstructorCache[type];
                        ReflectionUtils.ConstructorDelegate local3 = local1;
                        if (local1 == null)
                        {
                            ReflectionUtils.ConstructorDelegate local2 = local1;
                            Type[] typeArguments = new Type[] { genericListElementType };
                            local3 = this.ConstructorCache[typeof(List <>).MakeGenericType(typeArguments)];
                        }
                        object[] args = new object[] { list2.Count };
                        list3 = (IList)local3(args);
                        foreach (object obj5 in list2)
                        {
                            list3.Add(this.DeserializeObject(obj5, genericListElementType));
                        }
                    }
                    obj2 = list3;
                }
            }
            return(obj2);
        }
示例#4
0
        public virtual object DeserializeObject(object value, Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (value != null && type.IsInstanceOfType(value))
            {
                return(value);
            }
            string text = value as string;

            if (type == typeof(Guid) && string.IsNullOrEmpty(text))
            {
                return(default(Guid));
            }
            if (value == null)
            {
                return(null);
            }
            object obj = null;

            if (text != null)
            {
                if (text.Length != 0)
                {
                    if (type == typeof(DateTime) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTime)))
                    {
                        return(DateTime.ParseExact(text, PocoJsonSerializerStrategy.Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal));
                    }
                    if (type == typeof(DateTimeOffset) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTimeOffset)))
                    {
                        return(DateTimeOffset.ParseExact(text, PocoJsonSerializerStrategy.Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal));
                    }
                    if (type == typeof(Guid) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid)))
                    {
                        return(new Guid(text));
                    }
                    if (type == typeof(Uri))
                    {
                        bool flag = Uri.IsWellFormedUriString(text, UriKind.RelativeOrAbsolute);
                        Uri  result;
                        if (flag && Uri.TryCreate(text, UriKind.RelativeOrAbsolute, out result))
                        {
                            return(result);
                        }
                        return(null);
                    }
                    else
                    {
                        if (type == typeof(string))
                        {
                            return(text);
                        }
                        return(Convert.ChangeType(text, type, CultureInfo.InvariantCulture));
                    }
                }
                else
                {
                    if (type == typeof(Guid))
                    {
                        obj = default(Guid);
                    }
                    else if (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid))
                    {
                        obj = null;
                    }
                    else
                    {
                        obj = text;
                    }
                    if (!ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid))
                    {
                        return(text);
                    }
                }
            }
            else if (value is bool)
            {
                return(value);
            }
            bool flag2          = value is long;
            bool flag3          = value is ulong;
            bool flag4          = value is double;
            Type underlyingType = Nullable.GetUnderlyingType(type);

            if (underlyingType != null && PlayFabSimpleJson.NumberTypes.IndexOf(underlyingType) != -1)
            {
                type = underlyingType;
            }
            bool flag5  = PlayFabSimpleJson.NumberTypes.IndexOf(type) != -1;
            bool isEnum = type.IsEnum;

            if ((flag2 && type == typeof(long)) || (flag3 && type == typeof(ulong)) || (flag4 && type == typeof(double)))
            {
                return(value);
            }
            if ((flag2 || flag3 || flag4) && isEnum)
            {
                return(Enum.ToObject(type, Convert.ChangeType(value, Enum.GetUnderlyingType(type), CultureInfo.InvariantCulture)));
            }
            if ((flag2 || flag3 || flag4) && flag5)
            {
                return(Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
            }
            IDictionary <string, object> dictionary = value as IDictionary <string, object>;

            if (dictionary == null)
            {
                IList <object> list = value as IList <object>;
                if (list != null)
                {
                    IList <object> list2 = list;
                    IList          list3 = null;
                    if (type.IsArray)
                    {
                        list3 = (IList)this.ConstructorCache[type](new object[]
                        {
                            list2.Count
                        });
                        int num = 0;
                        foreach (object value2 in list2)
                        {
                            list3[num++] = this.DeserializeObject(value2, type.GetElementType());
                        }
                    }
                    else if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || ReflectionUtils.IsAssignableFrom(typeof(IList), type) || type == typeof(object))
                    {
                        Type genericListElementType = ReflectionUtils.GetGenericListElementType(type);
                        ReflectionUtils.ConstructorDelegate constructorDelegate = null;
                        if (type != typeof(object))
                        {
                            constructorDelegate = this.ConstructorCache[type];
                        }
                        if (constructorDelegate == null)
                        {
                            constructorDelegate = this.ConstructorCache[typeof(List <>).MakeGenericType(new Type[]
                            {
                                genericListElementType
                            })];
                        }
                        list3 = (IList)constructorDelegate(new object[0]);
                        foreach (object value3 in list2)
                        {
                            list3.Add(this.DeserializeObject(value3, genericListElementType));
                        }
                    }
                    obj = list3;
                }
                return(obj);
            }
            IDictionary <string, object> dictionary2 = dictionary;

            if (ReflectionUtils.IsTypeDictionary(type))
            {
                Type[] genericTypeArguments = ReflectionUtils.GetGenericTypeArguments(type);
                Type   type2 = genericTypeArguments[0];
                Type   type3 = genericTypeArguments[1];
                Type   key   = typeof(Dictionary <, >).MakeGenericType(new Type[]
                {
                    type2,
                    type3
                });
                IDictionary dictionary3 = (IDictionary)this.ConstructorCache[key](new object[0]);
                foreach (KeyValuePair <string, object> keyValuePair in dictionary2)
                {
                    dictionary3.Add(keyValuePair.Key, this.DeserializeObject(keyValuePair.Value, type3));
                }
                obj = dictionary3;
            }
            else if (type == typeof(object))
            {
                obj = value;
            }
            else
            {
                obj = this.ConstructorCache[type](new object[0]);
                foreach (KeyValuePair <string, KeyValuePair <Type, ReflectionUtils.SetDelegate> > keyValuePair2 in this.SetCache[type])
                {
                    object value4;
                    if (dictionary2.TryGetValue(keyValuePair2.Key, out value4))
                    {
                        value4 = this.DeserializeObject(value4, keyValuePair2.Value.Key);
                        keyValuePair2.Value.Value(obj, value4);
                    }
                }
            }
            if (ReflectionUtils.IsNullableType(type))
            {
                return(ReflectionUtils.ToNullableType(obj, type));
            }
            return(obj);
        }