internal bool ConverterExistsForType(Type t, out JavaScriptConverter converter)
 {
     converter = this.GetConverter(t);
     return(converter != null);
 }
Ejemplo n.º 2
0
        private static object ConvertDictionaryToObject(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            object obj2;
            Type   t  = type;
            string id = null;
            object o  = dictionary;

            if (dictionary.TryGetValue("__type", out obj2))
            {
                id = ConvertObjectToType(obj2, typeof(string), serializer) as string;
                if (id != null)
                {
                    if (serializer.TypeResolver != null)
                    {
                        t = serializer.TypeResolver.ResolveType(id);
                        if (t == null)
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    dictionary.Remove("__type");
                }
            }
            JavaScriptConverter converter = null;

            if ((t != null) && serializer.ConverterExistsForType(t, out converter))
            {
                return(converter.Deserialize(dictionary, t, serializer));
            }
            if ((id != null) || ((t != null) && IsClientInstantiatableType(t, serializer)))
            {
                o = Activator.CreateInstance(t);
            }
            List <string> list = new List <string>(dictionary.Keys);

            if ((((type != null) && type.IsGenericType) && (typeof(IDictionary).IsAssignableFrom(type) || (type.GetGenericTypeDefinition() == _idictionaryGenericType))) && (type.GetGenericArguments().Length == 2))
            {
                Type type3 = type.GetGenericArguments()[0];
                if ((type3 != typeof(string)) && (type3 != typeof(object)))
                {
                    throw new InvalidOperationException();//string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DictionaryTypeNotSupported, new object[] { type.FullName }));
                }
                Type        type4       = type.GetGenericArguments()[1];
                IDictionary dictionary2 = null;
                if (IsClientInstantiatableType(type, serializer))
                {
                    dictionary2 = (IDictionary)Activator.CreateInstance(type);
                }
                else
                {
                    dictionary2 = (IDictionary)Activator.CreateInstance(_dictionaryGenericType.MakeGenericType(new Type[] { type3, type4 }));
                }
                if (dictionary2 != null)
                {
                    foreach (string str2 in list)
                    {
                        dictionary2[str2] = ConvertObjectToType(dictionary[str2], type4, serializer);
                    }
                    return(dictionary2);
                }
            }
            if ((type != null) && !type.IsAssignableFrom(o.GetType()))
            {
                if (type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, s_emptyTypeArray, null) == null)
                {
                    throw new MissingMethodException(); //string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_NoConstructor, new object[] { type.FullName }));
                }
                throw new InvalidOperationException();  //string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DeserializerTypeMismatch, new object[] { type.FullName }));
            }
            foreach (string str3 in list)
            {
                object propertyValue = dictionary[str3];
                AssignToPropertyOrField(propertyValue, o, str3, serializer);
            }
            return(o);
        }