2010-02-11 5 views
2

(우수) PropertyBag 클래스에서 파생되었으며 DataContractJsonSerializer를 사용하여 Json에 직렬화하려고합니다. 불행히도 DataContractAttribute로 생성 되었음에도 불구하고 동적 속성이 JSON에 표시되지 않습니다. 어떻게 이러한 동적 속성을 직렬화 할 수 있습니까?DataContractJsonSerializer가 DataMemberAttribute를 보지 못함

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

namespace JsonProperties 
{ 
    [DataContract] 
    class MyClass : PropertyBag 
    { 
     static MyClass() 
     { 
      Attribute att_lat = new DisplayNameAttribute("Latitude"); 
      Attribute att_data = new DataMemberAttribute(); 

      Attribute[] attribs_lat = { att_lat, att_data }; 

      MyClass.AddProperty("_Latitude", typeof(String), attribs_lat); 
     } 
     [DataMember] 
     public Decimal latitude 
     { 
      get { return (Decimal)this["_Latitude"]; } 
      set 
      { // Validation etc. 
       this["_Latitude"] = value; 
      } 
     } 
    } 

    class Program 
     { 
      static void Main(string[] args) 
      { 
       Attribute attr1 = new DisplayNameAttribute("Dynamic Note Property"); 
       Attribute attr2 = new DataContractAttribute(); 
       Attribute[] attrs = { attr1, attr2 }; 

       MyClass.AddProperty("Notes", typeof(String), attrs); 
       MyClass location = new MyClass(); 

       location.latitude = 0.1M; 
       location["Notes"] = "Some text"; 

       DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(MyClass)); 
       dcjs.WriteObject(Console.OpenStandardOutput(), location); 
       Console.ReadLine(); 
      } 
     } 
    } 

답변

1

"메모"속성은 JSON 직렬 변환기에서 선택하기 위해 DataMemberAttribute로 장식되어야합니다. 여기서는 DataContractAttirbute가 아닙니다.

관련 문제