WCF DataMember EmitDefaultValue on value type? (but set my own default value)

假装没事ソ 提交于 2019-12-02 01:52:19

EmitDefaultValue is true by default.

You can try to useDefaultValue attribute from System.ComponentModel but I'm not sure if it works.

I just tested DefaultValue attribute and it doesn't work. It means that you cannot change default value - default value of the data type will be always used.

If you want to set your Bar to true use:

[DataContract]
public class Foo
{
    [DataMember(EmitDefaultValue = false)
    public bool? Bar { get; set; }

    [OnDeserialized]
    private void SetValuesOnDeserialized(StreamingContext context)
    {
        if (!Bar.HasValue) 
        {
           Bar = true;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!