I am serializing objects to XML using System.Xml.Serialization
and this requires me to have parameterless constructors.
I am therefore trying to use Object Initialization Syntax to assign values to certain properties and then use the constructor logic to format these values as needs be before I serialize the objects to XML.
My problem is that the constructor runs before the properties are assigned their values. A simplified example is below:
class Program
{
static void Main(string[] args)
{
Foo myFoo = new Foo() { HelloWorld = "Beer", HelloWorldAgain = "More beer" };
Console.WriteLine(myFoo.HelloWorld);
Console.WriteLine(myFoo.HelloWorldAgain);
Console.ReadLine();
}
}
public class Foo : Bar
{
public string HelloWorld { get; set; }
public Foo()
{
Console.WriteLine("Foo Was Initialized");
Console.WriteLine(HelloWorld);
}
}
public abstract class Bar
{
public string HelloWorldAgain { get; set; }
public Bar()
{
Console.WriteLine("Bar was initialized");
Console.WriteLine(HelloWorldAgain);
}
}
This results in the following output:
As you can see the constructor logic runs, and then the properties are assigned values. I need this to work the other way around.
Is this possible?