0

Possible Duplicate:
Serializing an array of integers using XmlSerializer

I am writing my Windows 8 app in C# and upon trying to serialize my custom object I ran into an "There was an error reflecting type" error message. Upon looking into the inner exception I came across the fact that we can't serialize multidimensional arrays.

Am I doing something wrong or am I right?

If I'm right, is there a way to pack up my class of ints and int[,] arrays to send them to romaing storage? Thank you.

I'm using this .dll: http://winrtstoragehelper.codeplex.com/

Here is my code:

var objectStorageHelper = new ObjectStorageHelper<BaseballTeam>(StorageType.Roaming);
await objectStorageHelper.SaveAsync(team);

With BaseballTeam being the type, and team the specific object.

Community
  • 1
  • 1
mjhannaf
  • 53
  • 1
  • 2
  • 7
  • Can you show the code? I've never ran into this issue and according to this URL, multi-dimensional int arrays should be serializable: http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/90c98754-2580-404a-81ae-aedba5f2604d/ – Adam Plocher Sep 28 '12 at 23:02
  • 3
    What serialization method are you using? Arrays (multi-dimensional or not) will serialized with standard serializers so long as the element type is serializable. – Adam Robinson Sep 28 '12 at 23:02
  • @AdamRobinson The DataContractSerializer doesn't support multi-dimensional arrays. "Combining collection types (having collections of collections) is allowed. Jagged arrays are treated as collections of collections. Multidimensional arrays are not supported." [See under Advanced Collection Rules](http://msdn.microsoft.com/en-us/library/aa347850%28v=vs.100%29.aspx) – dee-see Sep 28 '12 at 23:13
  • well, since the majority of people say it does not support multiple arrays, just flatten yours to single dimensional: array[x * width + y] – Benjamin Danger Johnson Sep 28 '12 at 23:26
  • Multidimensional arrays are not supported; jagged arrays are (array[x][y]), so no need to flatten the structure. – carlosfigueira Sep 28 '12 at 23:48

2 Answers2

1

Not using that library; it uses the XmlSerializer, which does not support multidimensional arrays (See source at http://winrtstoragehelper.codeplex.com/SourceControl/changeset/view/60a57faeb36a#WinRtUtility%2fWinRtUtility%2fObjectStorageHelper.cs). You can still use some other serializer (IIRC JSON.NET supports multidimensional arrays, for example), and just write the serialized object to the roaming storage using the File API.

Or choose another library which does that for you.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
1

It's only mentioned explicitly in the DataContractSerializer documentation ("Combining collection types (having collections of collections) is allowed. Jagged arrays are treated as collections of collections. Multidimensional arrays are not supported." See under Advanced Collection Rules), but multi-dimensional arrays can't be serialized even by the XmlSerializer (a simple test will show you that pretty clearly even without the library you are using).

You can find another serializer or simply convert to a jagged array for serialization (and back to multi-dimensional when deserialized).

EDIT: A very similar question has been asked before and the answer can be a good workaround for you: Serializing an array of integers using XmlSerializer ,

Community
  • 1
  • 1
dee-see
  • 23,668
  • 5
  • 58
  • 91