我在序列化一个类时,因为这个类中有一个Font类型的字段,所以提示不能序列化(因为Font没有默认构造函数),那我怎么序列化这个类呢?或者把Font这个字段去掉不序列化也行,但是我用了NonSerialized也没用public class A
{
public A()
{} private string text;
private Font textFont; public string Text
{
get { return text; }
set { text = value; }
} public Font TextFont
{
get { return textFont; }
set { textFont = value; }
}
}请问上面的类怎么序列化,因为我要用到TextFont,所以希望public Font TextFont尽量可以用,谢谢!
{
public A()
{} private string text;
private Font textFont; public string Text
{
get { return text; }
set { text = value; }
} public Font TextFont
{
get { return textFont; }
set { textFont = value; }
}
}请问上面的类怎么序列化,因为我要用到TextFont,所以希望public Font TextFont尽量可以用,谢谢!
{
// your class members here public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("text", this.text);
}
}
看不懂啊,我要序列化的时候,不继承ISerializable,只要把Font那个字段就可以序列化了阿
我用的是序列化成XML文件的
和BinaryFormatter有什么大的区别吗?
a.Text = "10";
a.TextFont = this.Font;
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, a);
stream.Position = 0;
A a1 = (A)formatter.Deserialize(stream);[Serializable]
public class A
{
public A()
{} private string text;
private Font textFont; public string Text
{
get { return text; }
set { text = value; }
} public Font TextFont
{
get { return textFont; }
set { textFont = value; }
}
}
BinnaryFormatter默认序列化类实例的所有字段,而SoapFormatter和XmlSerializer默认只序列化类实例的所有public的字段和属性,并且属性要同时提供get和set方法。