Given this code
public override void Serialize(BaseContentObject obj)
{
string file = ObjectDataStoreFolder + obj.Slug + ".xml";
if(obj.GetType() == typeof(Page))
{
DataContractSerializer dcs = new DataContractSerializer(typeof (Page));
XmlDictionaryWriter myWriter =
XmlDictionaryWriter.CreateTextWriter(new FileStream(file, FileMode.CreateNew, FileAccess.Write),
Encoding.UTF8);
dcs.WriteObject(myWriter, obj);
myWriter.Close();
}
else if(obj.GetType() == typeof(Image))
{
DataContractSerializer dcs = new DataContractSerializer(typeof (Image));
...
...
}
}
is there a way to do something like this
DataContractSerializer dcs = new DataContractSerializer(obj.GetType()); // this fails however, compiler error
and get rid of those if() statements above? The constructor of DataContractSerializer there expects Type or Namespace but it does not work with obj.GetType().
My class hierarchy is as follows:
BaseContentClass (abstract)
Page (concrete, inherits BaseContentClass)
Image (concrete, inherits BaseContentClass)
...
?