Exemplo n.º 1
0
        /// <summary>
        /// 解析成对象
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="properties">对象属性列表</param>
        /// <param name="jsonObject">Json对象</param>
        /// <returns></returns>
        public object ConvertToObject(Type type, List <PropertyInfo> properties, IJsonObject jsonObject)
        {
            if (jsonObject.Type != JsonType.Content)
            {
                if (type.IsAssignableFrom(jsonObject.Value.GetType()))
                {
                    return(jsonObject.Value);
                }
                throw new JsonDeserializationException(jsonObject, type, "Json对象不能把" + jsonObject.Type.ToString() + "类型不能解析成object类型");
            }
            if (type.IsAssignableFrom(jsonObject.GetType()))
            {
                return(jsonObject);
            }
            object obj = System.Activator.CreateInstance(type);
            Dictionary <string, JsonObject> dictionary = (Dictionary <string, JsonObject>)jsonObject.Value;

            foreach (var item in properties)
            {
                JsonObject jsonitem = null;
                dictionary.TryGetValue(item.Name, out jsonitem);
                if (jsonitem == null)
                {
                    continue;
                }
                object objItem = SwitchDeserializationMethod(item.PropertyType, jsonitem);
#if NET35 || NET40
                item.SetValue(obj, objItem, null);
#else
                item.SetValue(obj, objItem);
#endif
            }
            return(obj);
        }