【发布时间】:2010-09-03 11:30:44
【问题描述】:
我正在阅读有关序列化的内容,到目前为止,我一直在搞乱 BinaryFormatter 和 SoapFormatter。到目前为止,一切都很好 - 一切都完美地序列化和反序列化。
但是,当我尝试下面的代码时,我希望我的数据文件不包含 Name 的信息 - 它确实包含。当我在字段上指定 SoapIgnore 时,它为什么会包含它?
我还尝试在年龄字段上使用SoapAttribute("SomeAttribute"),但这也没有任何区别。框架版本设置为 2.0,但同样的事情发生在 3.5 和 4.0。
using System;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;
using System.Xml.Serialization;
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Age = 42;
p.Name = "Mr Smith";
SoapFormatter formatter = new SoapFormatter();
FileStream fs = new FileStream(@"c:\test\data.txt", FileMode.Create);
formatter.Serialize(fs, p);
}
}
[Serializable]
class Person
{
public int Age;
[SoapIgnore]
public string Name;
}
【问题讨论】:
标签: c# .net serialization soap-serialization