Deserialize shared collections - go empty

I have a settings object for my application that has two collections. Collections are simple lists that contain a collection of property packages. When I serialize it, everything is saved without problems:

XmlSerializer x = new XmlSerializer(settings.GetType());
TextWriter tw = new StreamWriter(@"c:\temp\settings.cpt");
x.Serialize(tw, settings);

However, when I deserialize it, everything is restored, with the exception of two collections (checked by setting a breakpoint on the setters:

XmlSerializer x = new XmlSerializer(typeof(CourseSettings));
XmlReader tr = XmlReader.Create(@"c:\temp\settings.cpt");
this.DataContext = (CourseSettings)x.Deserialize(tr);

What can cause this? Everything is pretty vanilla ... here is a snippet from the settings object ... omitting most of it. PresentationSourceDirectory works just fine, but the PresentationModules installer did not hit:

private string _presentationSourceDirectory = string.Empty;
public string PresentationSourceDirectory {
  get { return _presentationSourceDirectory; }
  set {
    if (_presentationSourceDirectory != value) {
      OnPropertyChanged("PresentationSourceDirectory");
      _presentationSourceDirectory = value;
    }
  }
}

private List<Module> _presentationModules = new List<Module>();
public List<Module> PresentationModules {
  get {
    var sortedModules = from m in _presentationModules
                        orderby m.ModuleOrder
                        select m;
    return sortedModules.ToList<Module>();
  }
  set {
    if (_presentationModules != value) {
      _presentationModules = value;
      OnPropertyChanged("PresentationModules");
    }
  }
}
+3
source share
2 answers

, . . Deserializer #Add() Exiting. . . MSDN.
, - linq. [XmlIgnore] , XmlSerializer .

[XmlIgnore]
public List<Module> PresentationModules {

, :

private List<Module> _presentationModules = new List<Module>();
public List<Module> PresentationModulesList {
  get { return _presentationModules; }
}

PresentationModules , . , setter .

+1

- . , - , , , .

0

Source: https://habr.com/ru/post/1739687/


All Articles