Serialization problem, "is not marked as serializable" error for UserControl

N

Nirdesh

Hi,

I am serializing a custom class holding some data for my project.

This data internally contains a class which contains an event

public delegate void MemberModifiedEventHandler(object sender,
MemberModifiedEventArgs e);

/// <summary>

/// Occurs on modification in a group.

/// </summary>

public event MemberModifiedEventHandler OnMembersModified;



This sender brings in a CutomPage derived from
System.Windows.Forms.UserControl.

I do not need to serialize this class as it contains only UI and has
got nothing to be saved and serialized.

But when i serialize the data it gives the error "CutomPage is not
marked as serializable".

How can i come across this problem without serializing this page.



NonSerializable attribute does not work for events and classes.



How can i serialize my data without serializing these usercontrol
clases.



Waiting for a kind reply.

Thanks & Regards

Nirdesh Dabas

(e-mail address removed)
 
F

Frans Bouma [C# MVP]

Nirdesh said:
Hi,

I am serializing a custom class holding some data for my project.

This data internally contains a class which contains an event

public delegate void MemberModifiedEventHandler(object sender,
MemberModifiedEventArgs e);

/// <summary>

/// Occurs on modification in a group.

/// </summary>

public event MemberModifiedEventHandler OnMembersModified;



This sender brings in a CutomPage derived from
System.Windows.Forms.UserControl.

I do not need to serialize this class as it contains only UI and has
got nothing to be saved and serialized.

But when i serialize the data it gives the error "CutomPage is not
marked as serializable".

How can i come across this problem without serializing this page.



NonSerializable attribute does not work for events and classes.



How can i serialize my data without serializing these usercontrol
clases.

This is likely due to the event handler in the control bound to the
event. This means that the event containing class has a reference to
the handler containing class.

By default .net serializes all these things, so if you want to get rid
of this, you have to implement ISerializable on your data containing
class.

FB




--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
M

Marc Gravell

By default .net serializes all these things, so if you want to get
rid
of this, you have to implement ISerializable on your data containing
class.

Or just tell it not to; for instance:

[field: NonSerialized]
public event EventHandler SomeEvent;

(the "field:" refers to the backing-field that .NET generates to hold
the delegate for this event)
 
F

Frans Bouma [C# MVP]

Marc said:
By default .net serializes all these things, so if you want to get
rid of this, you have to implement ISerializable on your data
containing class.

Or just tell it not to; for instance:

[field: NonSerialized]
public event EventHandler SomeEvent;

(the "field:" refers to the backing-field that .NET generates to hold
the delegate for this event)

Hmm, didn't know that, is that new in .net 2.0? I fought with event
handler serialization in .net 1.x mostly and after that it became more
or less a default for me to implement ISerializable.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
M

Marc Gravell

Hmm, didn't know that, is that new in .net 2.0?

Well, I've ditched my 1.1 tools so I can't check, but
NonSerializedAttribute appears in MSDN2 with a 1.1 entry so I assume
so. Very easily missed, however. The "field" bit I only found out
about a little while ago; until then I'd been adding a field / add /
remove to achieve the same (which is pretty-much what the field:
syntax does), something like [notepad code; not checked]:

[NonSerialized]
private EventHandler _someEvent;

public event EventHandler SomeEvent {
add {_someEvent += value;}
remove {_someEvent -= value;}
}

Marc
 
F

Frans Bouma [C# MVP]

Marc said:
Hmm, didn't know that, is that new in .net 2.0?

Well, I've ditched my 1.1 tools so I can't check, but
NonSerializedAttribute appears in MSDN2 with a 1.1 entry so I assume
so. Very easily missed, however. The "field" bit I only found out
about a little while ago; until then I'd been adding a field / add /
remove to achieve the same (which is pretty-much what the field:
syntax does), something like [notepad code; not checked]:

[NonSerialized]
private EventHandler _someEvent;

public event EventHandler SomeEvent {
add {_someEvent += value;}
remove {_someEvent -= value;}
}

I did know about NonSerialized, but not about the 'field:' fragment.
:) That one alone could have saved me a lot of time! :) Oh well...

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top