4

I am having problems saving serialized data from a nested custom class list. How do you properly serialize these classes?

[System.Serialize]
public class OuterClass() {
  List<InnerClass> someList = new List<InnerClass>();
  public OuterClass() {}
}
public class InnerClass() {
  public int someInt;
  public InnerClass(int _someInt) {
    someInt = _someInt;
  }
}
Darren
  • 68,902
  • 24
  • 138
  • 144
Abdulla
  • 1,117
  • 4
  • 21
  • 44

3 Answers3

2

Mark both your class with the [Serializable] attribute

Darren
  • 68,902
  • 24
  • 138
  • 144
  • Thank you for the answer, that does solve one of the problems in my serialization code. However, it seems that the issue runs deeper. Follow-up question here: http://stackoverflow.com/questions/10362513/c-sharp-polymorphism-with-serialized-data – Abdulla Apr 28 '12 at 10:06
0

The InnerClass class should have the [System.Serialize] attribute as well to be able to serialize it. Something along these lines:

[System.Serialize]
public class OuterClass() {
  List<InnerClass> someList = new List<InnerClass>();

  [System.Serialize]
  public class InnerClass() {
    public int someInt;
  }
}

On a side note, your InnerClass is not nested. It should be defined inside the OuterClass if you want to call it "nested class"

GETah
  • 20,922
  • 7
  • 61
  • 103
0

Both the containing and the contained class must be decorated with the [Serializable] attribute.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108