2012-11-20 83 views
2

嘗試在Windows上的Visual Studio中的代碼以確保。單聲道不尊重System.Runtime.Serialization.DataMemberAttribute EmitDefaultValue設置

單聲道框架似乎不兌現DataMemberAttributeEmitDefaultValue參數。使用下面的代碼:

using System; 
using System.IO; 
using System.Runtime.Serialization.Json; 
using System.Runtime.Serialization; 

namespace MyApp 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      Cereal specialK = new Cereal(); 
      specialK.TheValue="This is a what?"; 

      var ser = new DataContractJsonSerializer(typeof(Cereal)); 
      MemoryStream stm = new MemoryStream(); 
      ser.WriteObject(stm, specialK); 
      string json = System.Text.Encoding.UTF8.GetString(stm.ToArray()); 

      Console.WriteLine(json); 
      Console.ReadLine(); 
     } 
    } 

    [DataContract] 
    class Cereal 
    { 
     [DataMember(Name="set_on_serialize")] 
     private string _setOnSerialize = string.Empty; 

     [DataMember(Name = "default_export", EmitDefaultValue = false)] 
     private string _default_null; 

     public Cereal() { } 

     [DataMember(Name = "out_value")] 
     public string TheValue 
     { 
      get; 
      set; 
     } 

     [OnSerializing] 
     void OnSerializing(StreamingContext content) 
     { 
      this._setOnSerialize = "A brick!"; 
     } 
    } 
} 

單聲道的輸出結果中:

{"default_export":null,"out_value":"This is a what?","set_on_serialize":""} 

的default_export屬性被導出爲空,但不應該是輸出,因爲它是string類型的默認值。

從VS在Windows

正確的輸出是:

{"out_value":"This is a what?","set_on_serialize":"A brick!"} 

這是單聲道的錯誤還是我失去了一些東西?

+1

在http://www.mono-project.com/Bugs – pylover

+0

上報告此錯誤另外,請在您的問題中包括版本號,特別是在指出可能的錯誤時。 – skolima

+0

已記錄。謝謝。 – Russell

回答