开发者

Is it possible to have optional DataMembers in WCF?

开发者 https://www.devze.com 2023-03-11 21:20 出处:网络
If I have the following class: [DataContract] public class GetColorsRS { [DataMember(Name = \"Colors\", Order = 0, IsRequired=true)]

If I have the following class:

[DataContract]
public class GetColorsRS
{
    [DataMember(Name = "Colors", Order = 0, IsRequired=true)]
    public List<Color> Colors { get; set; }

    [DataMember(Name = "Errors", Order = 1, IsRequired=false)]
    public List<Error> Errors { get; set; }
}

If no errors are found in the request, I want to send back a response that does not have an Errors node, however, it passes back an Errors node that is empty. I thought this is what the IsRequired was for?

Just noticed EmitDefaultValue, is this what开发者_StackOverflow中文版 I am looking for?


I have determined that EmitDefaultValue should be set to false if I don't want to serialize the default value of the DataMember.

 [DataMember(Name = "Errors", Order = 1, IsRequired=false,EmitDefaultValue=false)]
 public List<Error> Errors { get; set; }
0

精彩评论

暂无评论...
验证码 换一张
取 消