views:

55

answers:

5

I keep getting a 'Type XXX is not marked as serializable' exception when I try to serialize an object of mine. It might sound silly, but my problem is that I can't seem to find any references to an object of type XXX anywhere on the object graph (using the debugger hover windows). Does anyone know a good way to scan the object graph for anything of this type?

It's a complex object graph (100s of levels deep), so I'm sure that some there must be a field of tye XXX somewhere, but I just can't find one.

+1  A: 

Do any of your objects derive from Type XXX, or do any of the properties of the type you are attempting to serialize derive from Type XXX?

Nick
It's a complex object graph (100s of levels deep), so I'm sure that some of the fields are of that type, but I just can't find them.
skb
Truly not trying to be Snarky, but have you thought of Find All References or Find in Solution in Visual Studio?
Nick
A: 

You might start checking inner exceptions of inner exceptions. Usually when I've had serialization problems, I'll wind up with 3 or 4 nested inner exceptions before I find out it was property A of class B which was a property in class C, which was in a list somewhere.

taylonr
+1  A: 

Also look for events; events followed during traversal of the object graph, if you have things connected to those events that cannot be serialized, you'll get this exception. You can mark the backing field of the event as non-serializable to overcome this issue:

[field:NonSerializable]
event MyEventHandler MyEvent;

Barring that, it might help us if you posted the code for your class.

Josh Petrie
+2  A: 

If the exceptions don't give you enough information, the [XmlIgnore] attribute is very useful for tracking down the culprit.

Throw it on everything at the object you are trying to serialize. Then remove it one at a time. When the object no longer serializes, you know the problem lies in that property not being serializable. Drill into that class, mark it all as [XmlIgnore], and repeat.

Eventually you'll find it.

Chad
+1  A: 

Your other option is actually to debug the serialization code. The way to do this is to put a try/catch around your call to Serialize, and have a breakpoint in your catch. Then when the exception get's thrown, drag the yellow bar to one line above where Serialize get's called and this time, you'll be able to set a breakpoint inside the Serialization code if you know where the code is stored.

That's because the Serialize method actually gets generated and compiled on the first run. You can see more details of how to do this in this blog posting.

Nick