當採用序列Newtonsoft的JSON(8.0.0版本)DependencyObject
衍生類的.Net/C#我注意到,它總是序列化DependencyObjectType
和Dispatcher
對象與我所有的衍生類的屬性的一起。由於DependencyObjectType
和Dispatcher
對象來自我繼承的DependencyObject
類,如何添加智能標記或屬性(如[JsonIgnore]
)以防止它們被序列化?或者我可能想到這是錯誤的方式?如何防止使用Newtonsoft JSON對DependencyObjects上的DependencyObjectType信息進行序列化?
實例類代碼:
public class MyClass : DependencyObject
{
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));
}
串行代碼:
JsonSerializer serializer = new JsonSerializer();
using (StreamWriter sw = new StreamWriter(filePath))
using (JsonWriter jw = new JsonTextWriter(sw))
{
serializer.Serialize(jw, MyInstance);
}
當連載我得到這個龐大的BLOB(所有,但最上面一行是不需要的):
{
"MyProperty": 0,
"DependencyObjectType": {
"Id": 148,
"SystemType": "MyProject.MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"BaseType": {
"Id": 0,
"SystemType": "System.Windows.DependencyObject, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxx",
"BaseType": null,
"Name": "DependencyObject"
},
"Name": "MyClass"
},
"IsSealed": false,
"Dispatcher": {
"Thread": {
"ManagedThreadId": 17,
"ExecutionContext": null,
"Priority": 2,
"IsAlive": true,
"IsThreadPoolThread": true,
"IsBackground": true,
"ThreadState": 4,
"ApartmentState": 1,
"CurrentUICulture": "en-US",
"CurrentCulture": "en-US",
"Name": null
},
"HasShutdownStarted": false,
"HasShutdownFinished": false,
"Hooks": {}
}
}