Abdulla
Abdulla

Reputation: 1127

C# Serialize Nested Custom Class List

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;
  }
}

Upvotes: 4

Views: 1982

Answers (3)

Darren
Darren

Reputation: 70728

Mark both your class with the [Serializable] attribute

Upvotes: 2

Dennis Traub
Dennis Traub

Reputation: 51634

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

Upvotes: 0

GETah
GETah

Reputation: 21409

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"

Upvotes: 0

Related Questions