TheCheatsrichter TheCheatsrichter - 1 month ago 7
C# Question

XMl Deserializise StackOverflowException

I am trying to deserialize a class from an allready saved .xml file.The problem is as soon as I use

get{...} set{...};
the code throws a StackOverflow Exception. Do I create an infinite loop with this piece of code? Because I couldn't track down a loop with halting points.

The class:

[Serializable]
public class AddOn
{
[System.Xml.Serialization.XmlElement("Name")]
public string Name { set; get; }

[XmlIgnore]
public ProcessStartInfo Info {
set { Info = value; }
get {
Info.Arguments = args;
Info.Arguments = Path;
return Info;
}
}

[System.Xml.Serialization.XmlElement("Arguments")]
public string args { set; get; }
[System.Xml.Serialization.XmlElement("Path")]
public string Path { set; get; }

[XmlIgnore]
public List<Process> ChildProcess = new List<Process>();

public AddOn(string Name, ProcessStartInfo Info)
{
this.Name = Name;
this.Info = Info;
args = Info.Arguments;
Path= Info.FileName;
}

public AddOn() { }
}


Where the

[XmlIgnore]
public ProcessStartInfo Info {
set { Info = value; }
get {
Info.Arguments = args;
Info.Arguments = Path;
return Info;
}
}


get{...}
function throws the StackOverflowException.

I do Ignore the ProcessStartInfo because I was unable to serialize this type in the first place and therefore created the two strings args and Path.

Code which should return the deserialized List of Addons:

XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<AddOn>));

StreamReader reader = new StreamReader(path);
var addons = (ObservableCollection<AddOn>)serializer.Deserialize(reader);
reader.Close();
return addons;

Answer

this call

 public ProcessStartInfo **Info** {
    **set { Info = value; }**
    get {
        Info.Arguments = args;
        Info.Arguments = Path;
        return Info;
    }
}

calls itself leading to the overflow