Newtonsoft.Json 中“检测到自引用循环”

spi*_*nt0 3 c# json.net

对于这段代码...

private void Label1_MouseUp(object sender, RoutedEventArgs e)
{
    Newtonsoft.Json.Linq.JObject.FromObject(e).ToString();
}
Run Code Online (Sandbox Code Playgroud)

我收到这个错误...

Self referencing loop detected with type 'System.Windows.Documents.Run'. Path 'MouseDevice.Target.Inlines[0].SiblingInlines'.
Run Code Online (Sandbox Code Playgroud)

还有很多其他类似的问题,但我不知道如何在我的案例中实现解决方案(我正在学习 C#)。例如添加ReferenceLoopHandling = ReferenceLoopHandling.Ignore但不知道把它放在哪里。

(顺便说一句,我正在尝试找到一种打印调试信息的简单通用方法。)

Cap*_*pet 5

在您的问题中,您说您无法弄清楚将ReferenceLoopHandling = ReferenceLoopHandling.Ignore. 您可以将其放入对“FromObject”的调用中,如下所示:

JObject.FromObject(e, new JsonSerializer() {
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
Run Code Online (Sandbox Code Playgroud)

以下是 JObject.FromObject 文档的链接: http://james.newtonking.com/json/help/index.html ?topic=html/CreatingLINQtoJSON.htm