I’m writing a custom deserializer that will deserialize a list by deserializing each of the individual objects in the collection and then putting it together.
Basically my code looks like this:
//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);
But this gives an error on the SetValue because (for example) I am trying to put a List(Object) into a List(Int32). Note that this problem only occurs with collections. The following code:
Object new_object = MyDeserialization(file_name)
myField.SetValue(resultObject, new_object)
works just fine provided that the runtime type of the result of MyDeserialization(file_name) is actually compatible with the type of myField. What is the problem here, and is there a way to make the collection deserialization work? (I’ve tried replacing the List(Object) declaration with myField.FieldType and that won’t even compile.
Collections do not offer covariance… a
List<int>simply isn’t aList<object>(or v.v.). As such, you need to identify theT, for example like so (using theFieldInfo.FieldType) – and create the right type of list in the first place.For convenience, once created it may be simpler to use the non-generic
IListinterface:However; I must stress – writing a full (and robust) serializer is a lot of work. Do you have a specific reason? Many of the inbuilt serializers are pretty good – for example DataContractSerializer – or 3rd party, such as Json.Net, and (if I do say so myself) protobuf-net.