After some digging, it seems like there's an explanation for this behavior. When you use the DataContractSerializer
, the default serialization order inside the type is alphabetical, and if you're using a class hierarchy, the order is top down. If there's a mismatch in the serialization order, the members will be initialized to their default values. That's why everything worked out fine when I introduced the base class (because the service uses a base class for the type being serialized).
So, if you want to flatten the hierarchy on the client side, you'll need to resort to the Order
property of the DataMemberAttribute
. You can apply the correct order position to each property (so that it mimics what's being serialized on the server) or you can "group" them by giving the same value to the properties that belong to the each level of the hierarchy (and rely on the the default alphabetical order for the properties that have the same order value - which, btw, is base on the Name
property, if you're also setting it):
[DataContract(Namespace = "http://xxx.pt/Mercados", Name = "util")]
public class Utilizador {
[field: DataMember(Name = "Id", Order = 1)]
public int Id { get; set; }
[field: DataMember(Name = "ver", Order = 1)]
private byte[] Version { get; set; }
[field: DataMember(Name = "n", Order = 2)]
public string Nome { get; set; }
[field: DataMember(Name = "im", Order = 2)]
public int IdMercado { get; set; }
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…