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; 


 }


 ...


 }



原作者:
...