0

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

Consider the following:

[DataContract]
public class MyType {
   [DataMember(EmitDefaultValue = true)]
   public string MyStr = "DefVal";
}

In the code above, I can specify the default value to use after assignment.

Now consider the following:

[DataContract]
public class MyType {
   [DataMember(EmitDefaultValue = true)]
   public string MyStr {
      get { ... }
      set { ... }
   }
}

How can I specify the default value here???

Thankyou

Community
  • 1
  • 1
Andry
  • 16,172
  • 27
  • 138
  • 246

1 Answers1

0

EmitDefaultValue says that if you do not specify the value it should still add element with default value for .NET type. In case of string it will be something like:

<MyStr xsi:nil="true" />

Your first example doesn't set default value. It sets common value because it is assignment as any other and if your incoming message contains null it will overwrite that initial value.

If you need any special value used instead of null (before serialization or after deserialization) use methods decorated with [OnSerializing] and [OnDeserialized] attributes as described in linked duplicate.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670