skb
skb

Reputation: 31084

.NET Serialization Issue

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.

Upvotes: 0

Views: 141

Answers (5)

Nick
Nick

Reputation: 5945

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.

Upvotes: 1

CaffGeek
CaffGeek

Reputation: 22054

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.

Upvotes: 2

user197015
user197015

Reputation:

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.

Upvotes: 1

taylonr
taylonr

Reputation: 10790

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.

Upvotes: 0

Nick
Nick

Reputation: 5945

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?

Upvotes: 1

Related Questions