0

I'm having issues serializing arrays. These are arrays of objects unknown until runtime, except that they will be instances of the abstract class DataItem, which implements ISerializable and has the Serializable attribute.

These arrays are being serialized in an object called a DataPile when the datapile is serialized.

My serialization code looks like this:

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
    // ...
    List<DataItem> data = new List<DataItem>(dataCount);
    // ... build data list ...
    Type type = CurTypes[root.Key]; // determine type of DataItem
    Array dyndata = Array.CreateInstance(type, data.Count);
    for (int i = 0; i < data.Count; i++)
        dyndata.SetValue(data[i], i);
    info.AddValue("data", dyndata, type);
    // ...
}

My deserialization code looks like this:

public DataPile(SerializationInfo info, StreamingContext context)
{
    // ...
    // somestring refers to a string calculated via other objects
    // in info (specifically a string array)
    Type type = CurTypes["somestring"];        // typeof(class)
    Type type2 = CurTypes["somestring" + 'x']; // typeof(class[])
    dynamic data = info.GetValue("data", type2);
    items = (DataItem[])Array.CreateInstance(type, someSize);
    // populate items from data
    // ...
}

The variable items is of type DataItem[]. It is a private member of DataPile, and it stores the majority of the DataPile's actual information in a manner that remotely resembles hash tables. The variable root is of type DataNode, which isn't of much consequence here. The root's key (string) can be used to find the type of the derived class, and that's all that is relevent.

CurTypes is a dictionary. It's purpose should be clear.

The problem is that data is always deserialized as an array of nulls.

If I step through the code, in serialization I can see in the Immediate window that info.GetValue("data", whatever-type-I'm-currently-testing) is not an array of nulls, but instead exactly the array that I want it to be. However, once the same code is called in deserialization, the array has the correct number of elements, but all null.

I am using a binary formatter to serialize DataPile (rather, an object is serialized that has a number of DataPiles that it serializes, each with its own DataPile derived class). The object in question is being serialized to a stream created via File.Open(path, FileMode.Create). As far as I am aware, everything but this functionality is able to be serialized correctly to the same file, and likewise deserialized.

I've been having issues with this code for quite some time, and until recently, wasn't even able to get the code to execute without errors. I got to here by basing my code on this.

Edit: Nevermind.

Community
  • 1
  • 1
Joseph Fox
  • 11
  • 2
  • In your deserialization code if you change dynamic to var (ie instead of the runtime the compiler does the work of trying to resolve the types) does that give you more information on the deserialized types in the array..? – Andrew Jun 21 '14 at 19:56
  • possible duplicate of [Serialized objects disappearing (BinaryFormatter)](http://stackoverflow.com/questions/15789041/serialized-objects-disappearing-binaryformatter) – Mark Hall Jun 21 '14 at 20:03

0 Answers0