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