我想将继承自System.Exception的自定义异常对象进行JSON序列化。JsonConvert.SerializeObject似乎忽略了派生类型中的属性。这个问题可以很简单地说明:
class MyException : Exception {
public string MyProperty { get; set; }
}
class Program {
static void Main(string[] args) {
Console.WriteLine(JsonConvert.SerializeObject(new MyException {MyProperty = "foobar"}, Formatting.Indented));
//MyProperty is absent from the output. Why?
Console.ReadLine();
}
}
我已经尝试在正确的位置添加DataContract和DataMember属性了,但是它们没有起到帮助作用。我该如何让这个工作?