public object ConvertTo(Json value, Type conversionType) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (conversionType == null) { throw new ArgumentNullException(nameof(conversionType)); } if (!typeof(System.Collections.IList).IsAssignableFrom(conversionType)) { throw new ArgumentException("JsonListFormat 转换异常,传入的 [Type] 必须是实现了 ICollection<T> 的类型!"); } var itemType = MetaType.GetListItemType(conversionType); if (itemType == null) { throw new InvalidCastException(); } if (value == Json.Null) { return(null); } if (value is not JsonArray jarray) { throw new InvalidCastException(); } var collection = (System.Collections.IList)Activator.CreateInstance(conversionType, true); foreach (var json in jarray) { var item = JsonSerializer.Deserialize(json, itemType); collection.Add(item); } return(collection); }
/// <summary> /// 反序列化 IList /// </summary> /// <exception cref="JsonException"></exception> /// <exception cref="JsonFormatException"></exception> /// <exception cref="JsonSerializerException"></exception> private static IList CollectionDeserialize(JsonArray array, IList list) { var type = list.GetType(); if (type.IsArray) { var eleType = type.GetElementType(); var count = Math.Min(array.Count, list.Count); for (int i = 0; i < count; i++) { list[i] = Deserialize(array[i], eleType); } } else { var eleType = MetaType.GetListItemType(type); foreach (var jval in array) { list.Add(Deserialize(jval, eleType)); } } return(list); }