c# - Weird serialization error when entity is serializable -


i following error when trying serialize object:

type 'test.common.testobj`1+<>c__displayclass1`1[[test.common.testentity, test.common, version=1.0.0.0, culture=neutral, publickeytoken=null],[system.boolean, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089]]' in assembly 'test.common, version=1.0.0.0, culture=neutral, publickeytoken=null'  not marked serializable. 

i have [serializable] attribute both on testobj entity, it's base entities , entities related properties. else can cause "is not marked serializable" error?

and c__displayclass1`1 means??

googled c_displayclass1'1 , found following:

http://rantdriven.com/post/2011/07/09/the-mysterious-2b3c3ec__displayclass1.aspx

it has links , explanations how might event handler you're trying serialize.

this seems helpful comment:

the main issue has what's being serialized. default, event handlers internally represented compile-time generated field. field holds reference delegate(s) invoked when event raised.

using example above, exception caused because you're using anonymous method accesses resources beyond defined scope. under covers, class (probably called "<>c__displayclass1") created represent anonymous method. method doesn't marked [serializable()] attribute. when attempt serialize object attempts serialize fields , exception thrown.

you can fix code in 1 of several ways:

if want maintain serialization on event (which on default serializable class), easiest thing move 'addedmessage' variable anonymous method doesn't access local variables in containing scope.

if serialization of event isn't important you, can declare event field manually, marking [nonserialized()] attribute , use add , remove accessors on event block manage delegate references.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -