You can implement a ShouldSerializeAddress
method to decide whether or not the Address property should be serialized :
public bool ShouldSerializeAddress()
{
return Address != null
&& !String.IsNullOrEmpty(Address.street)
&& !String.IsNullOrEmpty(Address.town);
}
If the method exists with this signature, the serializer will call it before serializing the property.
Alternatively, you can implement an AddressSpecified
property which has the same role :
public bool AddressSpecified
{
get
{
return Address != null
&& !String.IsNullOrEmpty(Address.street)
&& !String.IsNullOrEmpty(Address.town);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…