【发布时间】:2010-11-06 19:43:21
【问题描述】:
我正在编写一个自定义反序列化程序,它将通过反序列化集合中的每个单独对象然后将它们放在一起来反序列化一个列表。
基本上我的代码是这样的:
//myField is a FieldInfo that represents the field we want to put the data in
//resultObject is the object we want the data to go into
List<Object> new_objects = new List<Object>();
foreach (String file_name in file_name_list)
{
Object field_object = MyDeserialization(file_name)
new_objects.Add(field_object)
}
myField.SetValue(resultObject, new_objects);
但这会在 SetValue 上出现错误,因为(例如)我试图将 List(Object) 放入 List(Int32)。请注意,此问题仅发生在集合中。以下代码:
Object new_object = MyDeserialization(file_name)
myField.SetValue(resultObject, new_object)
只要 MyDeserialization(file_name) 结果的运行时类型实际上与 myField 的类型兼容,就可以正常工作。这里有什么问题,有没有办法使集合反序列化工作? (我尝试用 myField.FieldType 替换 List(Object) 声明,但它甚至无法编译。
【问题讨论】:
标签: c# .net reflection serialization