others - c# - 将IList转换为泛型TResult,可不可以是IEnumerable或object


 public static TResult Deserialize<TResult>(StreamReader inputStream)


 {


 if (inputStream.EndOfStream) return default(TResult);



 if (typeof(TResult).IsEnumerable())


 {


 Type itemType = typeof(TResult).GetItemType();


 IList list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(itemType));



 MethodInfo deserializeMethod = 


 typeof(SimpleFixedWidthSerializer)


 .GetMethod("Deserialize", new[] { typeof(StreamReader) })


 .MakeGenericMethod(new[] { itemType });



 object item = null;


 do


 {


 item = deserializeMethod.Invoke(null, new[] { inputStream });


 if (item != null)


 list.Add(item);


 } while (item != null);



 list.Dump();


 return (TResult)list;


 }



 ...


 }



查看结果的Dump(),可以看到,它被正确地反序列化为Person的System.Collection.Generic.List。但我不知道如何从IListTResult,示例调用:


string testInput ="...";


using (MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(testInput)))


using (StreamReader sr = new StreamReader(mStream))


 SimpleFixedWidthSerializer.Deserialize<Person[]>(sr).Dump();



Unable to cast object of type 'System.Collections.Generic.List 1[UserQuery+Person]' to type 'Person[]'

如何将IList正确转换为TResult?

时间: 原作者:

  • 将list声明为List(可以使用var关键字),因为IList没有ToArray()方法
  • 将类型TResult限制为类(为了让ToArray()工作,需要这样做)
  • 在返回之前,通过调用ToArray()将List转换为数组
  • 使用as关键字进行强制转换,使它在运行时解析


public static TResult Deserialize<TResult>(StreamReader inputStream) where TResult : class


{


 if (inputStream.EndOfStream) return default(TResult);



 if (typeof(TResult).IsEnumerable())


 {


 Type itemType = typeof(TResult).GetItemType();


 var list = Activator.CreateInstance(typeof(List<>).MakeGenericType(itemType));



 MethodInfo deserializeMethod = 


 typeof(SimpleFixedWidthSerializer)


 .GetMethod("Deserialize", new[] { typeof(StreamReader) })


 .MakeGenericMethod(new[] { itemType });



 object item = null;


 do


 {


 item = deserializeMethod.Invoke(null, new[] { inputStream });


 if (item != null)


 list.Add(item);


 } while (item != null);



 list.Dump();


 return list.ToArray() as TResult;


 }



 ...


}



原作者:

这适用于Person[]或List


 public static TResult Deserialize<TResult>(StreamReader inputStream) where TResult : class


 {


 if (inputStream.EndOfStream) return default(TResult);



 if (typeof(TResult).IsEnumerable())


 {


 Type itemType = typeof(TResult).GetItemType();


 var list = Activator.CreateInstance(typeof(List<>).MakeGenericType(itemType));



 MethodInfo addMethod = list


 .GetType()


 .GetMethod("Add");



 MethodInfo methodInfo = typeof(SimpleFixedWidthSerializer)


 .GetMethod("Deserialize", new[] { typeof(StreamReader) })


 .MakeGenericMethod(new[] { itemType });


 object item = null;


 do


 {


 item = methodInfo.Invoke(null, new[] { inputStream });


 if (item != null)


 addMethod.Invoke(list, new[] { item });


 } while (item != null);



 MethodInfo toArray = list


 .GetType()


 .GetMethod("ToArray");



 if (typeof(TResult).IsArray)


 return toArray.Invoke(list, null) as TResult;


 return ((IEnumerable)list) as TResult; 


 }


 ...


 }



原作者:
...