I have a class:
public class MyClass
{
public MyClass(){}
}
I would like to be able to use an XMLSeralizer to Deserialize an XDocument directly in the constructor thus:
public class MyClass
{
private XmlSerializer _s = new XmlSerializer(typeof(MyClass));
public MyClass(){}
public MyClass(XDocument xd)
{
this = (MyClass)_s.Deserialize(xd.CreateReader());
}
}
Except I am not allowed to assign to “this” within the constructor.
Is this possible?
No, it’s not possible. Serializers create objects when they deserialize. You’ve already created an object. Instead, provide a static method to construct from an XDocument.