我正在尝试序列化一堆对象。我设计了一个较小的示例代码来调试问题,我希望在其中保存自定义链接的颜色,即CustomLink(Color.Blue)
以下是主要类 -
public class Factory
{
CustomLink customLink = new CustomLink(Color.Blue); // This color is not getting serialized
static Type[] xMLSerializationTypes = { typeof(CustomLink) };
static void Main(string[] args)
{
Factory factory = new Factory();
XmlSerializer serializer = new XmlSerializer(typeof(Factory), xMLSerializationTypes);
TextWriter textWriter = new StreamWriter(@"D:\test.xml");
serializer.Serialize(textWriter, factory);
textWriter.Close();
}
public CustomLink CustomLink
{
set { customLink = value; }
get { return customLink; }
}
}
以下是其余的课程 -
public class CustomLink : Link
{
public CustomLink() { }
public CustomLink(Color color)
{
GraphicProperty.Color = color;
}
}
public class Link
{
GraphicProperty graphicProperty = new GraphicProperty();
public GraphicProperty GraphicProperty
{
set { graphicProperty = value; }
get { return graphicProperty; }
}
}
public class GraphicProperty
{
Color color;
public GraphicProperty() { }
public Color Color
{
set { color = value; }
get { return color; }
}
}
下面是输出,它不包含任何颜色信息 -
<?xml version="1.0" encoding="utf-8"?>
<Factory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CustomLink>
<GraphicProperty>
<Color />
</GraphicProperty>
</CustomLink>
</Factory>