Deserialize to self
No, this is not possible using a constructor, you can't reassign this
.
Use a static method instead:
public static ArrivedDetails CreateFromString(string encrypted)
{
return DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
}
Call it:
ArrivedDetails details = ArrivedDetails.CreateFromString(encrypted);
You can not assign anything to "this". Change ArriveDetails to a static that return the deserialised object.
class ArrivedDetails
{
static ArrivedDetails Create(string encrypted)
{ return DataProtect.deserializeXML(...) }
}
You can archive this with reflection as follows.
var tmp = DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
foreach (var property in GetType().GetProperties())
if (property.GetCustomAttributes(typeof (XmlIgnoreAttribute), false).GetLength(0) == 0)
property.SetValue(this, property.GetValue(tmp, null), null);
This assigns the deserialized object to a temporal variable, and copy the value in each public property to this
with reflection. This snippet avoids to copy properties with the XmlIgnore attribute.