1。 xml serializer。这个是 asp。net 中 web service soap 请求的发送和接受默认使用的方式。指序列化对象的公共属性和成员。
2。 soap serializer . dotnet remoting 使用的对象传送方式。这个时候传送的对象要求有 serializable 标志。
3. binaryserializer 。同2, 只不过是二进制格式。
代码示例:
考虑一个 person 对象,会有一些属性比如fullname,唯一 id,电话(最多三个。)
[serializable]
public class phone
{
private string _phonenumber="";
private string _phonetype="";
public phone(string phonetype,string phonenumner)
{
_phonenumber=phonenumner;
_phonetype=phonetype;
}
public string phonedescp
{
get
{
return string.format("{0}\t{1}",_phonetype,_phonenumber);
}
}
public phone()
{
}
}
[serializable]
public class person
{
public person()
{
//
// todo: 在此处添加构造函数逻辑
//
}
//电话列表假设只能最多有三个
private phone [] _allphones=new phone[3];
//全称
private string fullname="";
//唯一的标识
private system.guid id=system.guid.newguid();
public string fullname
{
get{return fullname;}
set{fullname=value;}
}
/// <summary>
/// 标识
/// </summary>
public system.guid id
{
get
{
return id;
}
}
public phone this[int phoneindex]
{
get
{
system.diagnostics.debug.assert(phoneindex>=0 && phoneindex<=2);
return _allphones[phoneindex];
}
set
{
system.diagnostics.debug.assert(phoneindex>=0 && phoneindex<=2);
_allphones[phoneindex]=value;
}
}
}
如果用 xml serializer
只能看到一下结果。只序列华了 fullname
---------------------------
---------------------------
<?xml version="1.0" encoding="utf-16"?>
<person xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">
<fullname>montaquehou</fullname>
</person>
---------------------------
ok
---------------------------
注意 guid 和 phone 都没有序列化。长度为195 个字节。
而采用 soap 序列华则序列化了所有,2022 个字节。
---------------------------
---------------------------