I’m getting an error when I attempt to serialize an object:
Self referencing loop detected with type 'Namespace.MyClass'. Path ''.
I understand what this error means. Somewhere in my class, I have a loop where ClassA
references ClassB
, and ClassB
references ClassA
, resulting in an infinite loop that Newtonsoft cannot resolve.
There are countless questions on this site about this error. I know that I can ignore the properties with ReferenceLoopHandling.Ignore
. But that’s not my question. My question is this:
How do I actually find the reference loop, so I can try to fix the underlying class?
MY class is quite large, and extends classes from a library that I do not control. However, objects of the library classes have serialized just fine in the past, so it shouldn’t be the library causing the problem. But the actual exception doesn’t give me any useful information of which properties of my class are self-referencing.
Is there a way to detect this? Or do I just have to dig through my code manually?