WCF数据成员EmitDefaultValue价值类型? (不过我设置自己的默认值) [英] WCF DataMember EmitDefaultValue on value type? (but set my own default value)

查看:438
本文介绍了WCF数据成员EmitDefaultValue价值类型? (不过我设置自己的默认值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

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

2的问题:


  1. 什么是真正发生在这里,因为我的布尔真的不能为空,所以如果我发出的默认值又是什么?

  1. What really happens here because my bool can't really be null, so if I emit the default value then what?

我如何使它所以,如果有人传递一个消息,而酒吧一部分,那么它在我的服务器时,它在默认情况下设置为true,而不是假的?

How do I make it so that if someone passes a message without the Bar part then it my server sets it to true instead of false by default?

基本上是不需要我的的成员在SOAP消息被发送,如果不是我希望它默认为真,没有虚假。我不知道正确的组合,使我的消息大小效率(切出任何不必要的),然后该值默认为我想如果不是在消息中?


Basically, my bar member is not required to be transmitted over the soap message and if it isn't I want it to default to true, not false. I'm not sure of the proper combination to make my message sizes efficient (cut out anything unnecessary) and then default the value to what I want if it isn't in the message?

推荐答案

EmitDefaultValue 默认为true。

<击>您可以尝试使用默认值属性从 System.ComponentModel ,但我不知道它的工作原理。

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;
        }
    }
}

这篇关于WCF数据成员EmitDefaultValue价值类型? (不过我设置自己的默认值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆