简体   繁体   中英

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

[Serializable]属性标记两个班级

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"

包含类和包含类都必须用[Serializable]属性修饰。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM