How do I serialize a 'Type'?
I want to serialize to XML an object that has a property that is a type of an object. The idea is that when it is deserialized I can create an object of that type.
public class NewObject
{
}
[XmlRoot]
public class XmlData
{
private Type t;
public Type T
{
get { return t; }
set { t = value; }
}
}
static void Main(string[] args)
{
XmlData data = new XmlData();
data.T = typeof(NewObject);
try
{
XmlSerializer serializer = new XmlSerializer(typeof(XmlData));
try
{
using (FileStream fs = new FileStream("test.xml", FileMode.Create))
{
serializer.Serialize(fs, data);
}
}
catch (Exception ex)
{
}
}
catch (Exception ex)
{
}
}
I get this exception:
"The type ConsoleApplication1.NewObject was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."
Where do I put the [XmlInclude]? Is this even possible?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…