473,722 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialization of SortedList objects when object contains event

I have a class derived from a SortedList called SystemList that contains a
list of objects indexed with a string value.

The definition of the objects contained in the SortedList have a boolean
field and an event that is fired if the boolean field value changes.

The derived SortedList class has an override for the Add method. The Add
method adds an event handler for the object's event for each new item added
to the list. In the program, the events work fine and all seems well.

However, when I serialize and then try to deserialize the derived
SortedList, deserialization fails. If I rem out the line that adds the event
handler in the Add method, everything serializes and deserializes jsut fine.
But with it in I can't deserialize.

Can someone suggest what is going wrong here and how I can correct it?

Thanks,

Dennis

BTW, the code for the deserialization is:

BinaryFormatter bf = new BinaryFormatter ();

bf.AssemblyForm at = FormatterAssemb lyStyle.Simple;

IFormatter formatter = bf;

Stream stream = new FileStream(name , FileMode.Open, FileAccess.Read ,
FileShare.Read) ;

systemList = (SystemList)for matter.Deserial ize(stream);

fileName = name; // update doc's file name

stream.Close();
Nov 15 '05 #1
8 3125
Dennis,

When you have an event, you must store a reference to the target of the
event. In other words, a reference to the object that holds the method to
be called when the event is fired is stored by the object firing the event.

Because of this, when you serialize the object that fires the events, it
tries to serialize the event handlers as well.

To get around this, you will have to disconnect the event handlers, or
you could flag the event with the NonSerialized attribute (I am not sure if
that works). Either way, you have to prevent the event handlers from being
serialized.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:e4******** ******@TK2MSFTN GP12.phx.gbl...
I have a class derived from a SortedList called SystemList that contains a
list of objects indexed with a string value.

The definition of the objects contained in the SortedList have a boolean
field and an event that is fired if the boolean field value changes.

The derived SortedList class has an override for the Add method. The Add
method adds an event handler for the object's event for each new item added to the list. In the program, the events work fine and all seems well.

However, when I serialize and then try to deserialize the derived
SortedList, deserialization fails. If I rem out the line that adds the event handler in the Add method, everything serializes and deserializes jsut fine. But with it in I can't deserialize.

Can someone suggest what is going wrong here and how I can correct it?

Thanks,

Dennis

BTW, the code for the deserialization is:

BinaryFormatter bf = new BinaryFormatter ();

bf.AssemblyForm at = FormatterAssemb lyStyle.Simple;

IFormatter formatter = bf;

Stream stream = new FileStream(name , FileMode.Open, FileAccess.Read ,
FileShare.Read) ;

systemList = (SystemList)for matter.Deserial ize(stream);

fileName = name; // update doc's file name

stream.Close();

Nov 15 '05 #2
Nickolas:

I thought so, but trying the NonSerialize attribute does not work. That can
only be used on fields.

How do I remove/disconnect the event handlers? I would think -= but the
compiler doesn't like that.

Alternatively, is there a way to override the deserialization process and
add do something with the event handlers at that point?

Dennis

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:Or******** ******@tk2msftn gp13.phx.gbl...
Dennis,

When you have an event, you must store a reference to the target of the event. In other words, a reference to the object that holds the method to
be called when the event is fired is stored by the object firing the event.
Because of this, when you serialize the object that fires the events, it tries to serialize the event handlers as well.

To get around this, you will have to disconnect the event handlers, or
you could flag the event with the NonSerialized attribute (I am not sure if that works). Either way, you have to prevent the event handlers from being serialized.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:e4******** ******@TK2MSFTN GP12.phx.gbl...
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value.

The definition of the objects contained in the SortedList have a boolean field and an event that is fired if the boolean field value changes.

The derived SortedList class has an override for the Add method. The Add
method adds an event handler for the object's event for each new item

added
to the list. In the program, the events work fine and all seems well.

However, when I serialize and then try to deserialize the derived
SortedList, deserialization fails. If I rem out the line that adds the

event
handler in the Add method, everything serializes and deserializes jsut

fine.
But with it in I can't deserialize.

Can someone suggest what is going wrong here and how I can correct it?

Thanks,

Dennis

BTW, the code for the deserialization is:

BinaryFormatter bf = new BinaryFormatter ();

bf.AssemblyForm at = FormatterAssemb lyStyle.Simple;

IFormatter formatter = bf;

Stream stream = new FileStream(name , FileMode.Open, FileAccess.Read ,
FileShare.Read) ;

systemList = (SystemList)for matter.Deserial ize(stream);

fileName = name; // update doc's file name

stream.Close();


Nov 15 '05 #3
Dennis,

If that is the case, then declare your event like this:

// Declare the field that holds the delegate.
[NonSerializable]
private EventHandler mobjEventHandle rs = null;

// Declare the event.
public event EventHandler MyEvent
{
add
{
// Just add the event.
mobjEventHandle rs += value;
}
remove
{
// Just remove the event.
mobjEventHandle rs -= value;
}
}

And that should work.

If you use custom serialization, then you can set which values are
persisted.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
Nickolas:

I thought so, but trying the NonSerialize attribute does not work. That can only be used on fields.

How do I remove/disconnect the event handlers? I would think -= but the
compiler doesn't like that.

Alternatively, is there a way to override the deserialization process and
add do something with the event handlers at that point?

Dennis

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:Or******** ******@tk2msftn gp13.phx.gbl...
Dennis,

When you have an event, you must store a reference to the target of the
event. In other words, a reference to the object that holds the method to
be called when the event is fired is stored by the object firing the

event.

Because of this, when you serialize the object that fires the events, it
tries to serialize the event handlers as well.

To get around this, you will have to disconnect the event handlers,
or you could flag the event with the NonSerialized attribute (I am not sure

if
that works). Either way, you have to prevent the event handlers from

being
serialized.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:e4******** ******@TK2MSFTN GP12.phx.gbl...
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value.

The definition of the objects contained in the SortedList have a boolean field and an event that is fired if the boolean field value changes.

The derived SortedList class has an override for the Add method. The

Add method adds an event handler for the object's event for each new item

added
to the list. In the program, the events work fine and all seems well.

However, when I serialize and then try to deserialize the derived
SortedList, deserialization fails. If I rem out the line that adds the

event
handler in the Add method, everything serializes and deserializes jsut

fine.
But with it in I can't deserialize.

Can someone suggest what is going wrong here and how I can correct it?

Thanks,

Dennis

BTW, the code for the deserialization is:

BinaryFormatter bf = new BinaryFormatter ();

bf.AssemblyForm at = FormatterAssemb lyStyle.Simple;

IFormatter formatter = bf;

Stream stream = new FileStream(name , FileMode.Open, FileAccess.Read ,
FileShare.Read) ;

systemList = (SystemList)for matter.Deserial ize(stream);

fileName = name; // update doc's file name

stream.Close();



Nov 15 '05 #4
Thanks Nicholas,

Your comments are much appreciated!

Dennis
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:uo******** ******@TK2MSFTN GP11.phx.gbl...
Dennis,

If that is the case, then declare your event like this:

// Declare the field that holds the delegate.
[NonSerializable]
private EventHandler mobjEventHandle rs = null;

// Declare the event.
public event EventHandler MyEvent
{
add
{
// Just add the event.
mobjEventHandle rs += value;
}
remove
{
// Just remove the event.
mobjEventHandle rs -= value;
}
}

And that should work.

If you use custom serialization, then you can set which values are
persisted.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
Nickolas:

I thought so, but trying the NonSerialize attribute does not work. That can
only be used on fields.

How do I remove/disconnect the event handlers? I would think -= but the
compiler doesn't like that.

Alternatively, is there a way to override the deserialization process and
add do something with the event handlers at that point?

Dennis

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote

in
message news:Or******** ******@tk2msftn gp13.phx.gbl...
Dennis,

When you have an event, you must store a reference to the target of
the
event. In other words, a reference to the object that holds the
method to be called when the event is fired is stored by the object firing the event.

Because of this, when you serialize the object that fires the events,
it
tries to serialize the event handlers as well.

To get around this, you will have to disconnect the event

handlers, or you could flag the event with the NonSerialized attribute (I am not
sure if
that works). Either way, you have to prevent the event handlers from

being
serialized.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:e4******** ******@TK2MSFTN GP12.phx.gbl...
> I have a class derived from a SortedList called SystemList that

contains
a
> list of objects indexed with a string value.
>
> The definition of the objects contained in the SortedList have a

boolean
> field and an event that is fired if the boolean field value changes.
>
> The derived SortedList class has an override for the Add method. The

Add > method adds an event handler for the object's event for each new

item added
> to the list. In the program, the events work fine and all seems well. >
> However, when I serialize and then try to deserialize the derived
> SortedList, deserialization fails. If I rem out the line that adds the event
> handler in the Add method, everything serializes and deserializes jsut fine.
> But with it in I can't deserialize.
>
> Can someone suggest what is going wrong here and how I can correct it? >
> Thanks,
>
> Dennis
>
> BTW, the code for the deserialization is:
>
> BinaryFormatter bf = new BinaryFormatter ();
>
> bf.AssemblyForm at = FormatterAssemb lyStyle.Simple;
>
> IFormatter formatter = bf;
>
> Stream stream = new FileStream(name , FileMode.Open, FileAccess.Read ,
> FileShare.Read) ;
>
> systemList = (SystemList)for matter.Deserial ize(stream);
>
> fileName = name; // update doc's file name
>
> stream.Close();
>
>



Nov 15 '05 #5
Dennis,
Alternative to the event procedures as Nicholas showed, you can use the
field attribute modifier:

[field: NonSerialized]
public event EventHandler MyEvent;

Hope this helps
Jay

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
Nickolas:

I thought so, but trying the NonSerialize attribute does not work. That can only be used on fields.

How do I remove/disconnect the event handlers? I would think -= but the
compiler doesn't like that.

Alternatively, is there a way to override the deserialization process and
add do something with the event handlers at that point?

Dennis

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:Or******** ******@tk2msftn gp13.phx.gbl...
Dennis,

When you have an event, you must store a reference to the target of the
event. In other words, a reference to the object that holds the method to
be called when the event is fired is stored by the object firing the

event.

Because of this, when you serialize the object that fires the events, it
tries to serialize the event handlers as well.

To get around this, you will have to disconnect the event handlers,
or you could flag the event with the NonSerialized attribute (I am not sure

if
that works). Either way, you have to prevent the event handlers from

being
serialized.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:e4******** ******@TK2MSFTN GP12.phx.gbl...
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value.

The definition of the objects contained in the SortedList have a boolean field and an event that is fired if the boolean field value changes.

The derived SortedList class has an override for the Add method. The

Add method adds an event handler for the object's event for each new item

added
to the list. In the program, the events work fine and all seems well.

However, when I serialize and then try to deserialize the derived
SortedList, deserialization fails. If I rem out the line that adds the

event
handler in the Add method, everything serializes and deserializes jsut

fine.
But with it in I can't deserialize.

Can someone suggest what is going wrong here and how I can correct it?

Thanks,

Dennis

BTW, the code for the deserialization is:

BinaryFormatter bf = new BinaryFormatter ();

bf.AssemblyForm at = FormatterAssemb lyStyle.Simple;

IFormatter formatter = bf;

Stream stream = new FileStream(name , FileMode.Open, FileAccess.Read ,
FileShare.Read) ;

systemList = (SystemList)for matter.Deserial ize(stream);

fileName = name; // update doc's file name

stream.Close();



Nov 15 '05 #6

Hi Dennis,

Is your problem resolved?
If there is still any problem, please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Dennis C. Drumm" <de*******@prim acode.com>
| From: "Dennis C. Drumm" <de*******@prim acode.com>
| References: <e4************ **@TK2MSFTNGP12 .phx.gbl>
<Or************ **@tk2msftngp13 .phx.gbl>
<Ov************ **@TK2MSFTNGP10 .phx.gbl>
<uo************ **@TK2MSFTNGP11 .phx.gbl>
| Subject: Re: Serialization of SortedList objects when object contains
event
| Date: Wed, 15 Oct 2003 14:17:42 -0400
| Lines: 157
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eY************ *@TK2MSFTNGP10. phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 206-15-137-36.dialup.zipli nk.net 206.15.137.36
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1916 03
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Thanks Nicholas,
|
| Your comments are much appreciated!
|
| Dennis
|
|
| "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in
| message news:uo******** ******@TK2MSFTN GP11.phx.gbl...
| > Dennis,
| >
| > If that is the case, then declare your event like this:
| >
| > // Declare the field that holds the delegate.
| > [NonSerializable]
| > private EventHandler mobjEventHandle rs = null;
| >
| > // Declare the event.
| > public event EventHandler MyEvent
| > {
| > add
| > {
| > // Just add the event.
| > mobjEventHandle rs += value;
| > }
| > remove
| > {
| > // Just remove the event.
| > mobjEventHandle rs -= value;
| > }
| > }
| >
| > And that should work.
| >
| > If you use custom serialization, then you can set which values are
| > persisted.
| >
| > Hope this helps.
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - mv*@spam.guard. caspershouse.co m
| >
| > "Dennis C. Drumm" <de*******@prim acode.com> wrote in message
| > news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
| > > Nickolas:
| > >
| > > I thought so, but trying the NonSerialize attribute does not work.
That
| > can
| > > only be used on fields.
| > >
| > > How do I remove/disconnect the event handlers? I would think -= but
the
| > > compiler doesn't like that.
| > >
| > > Alternatively, is there a way to override the deserialization process
| and
| > > add do something with the event handlers at that point?
| > >
| > > Dennis
| > >
| > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om>
wrote
| > in
| > > message news:Or******** ******@tk2msftn gp13.phx.gbl...
| > > > Dennis,
| > > >
| > > > When you have an event, you must store a reference to the target
| of
| > > the
| > > > event. In other words, a reference to the object that holds the
| method
| > to
| > > > be called when the event is fired is stored by the object firing the
| > > event.
| > > >
| > > > Because of this, when you serialize the object that fires the
| > events,
| > > it
| > > > tries to serialize the event handlers as well.
| > > >
| > > > To get around this, you will have to disconnect the event
| handlers,
| > or
| > > > you could flag the event with the NonSerialized attribute (I am not
| sure
| > > if
| > > > that works). Either way, you have to prevent the event handlers
from
| > > being
| > > > serialized.
| > > >
| > > > Hope this helps.
| > > >
| > > >
| > > > --
| > > > - Nicholas Paldino [.NET/C# MVP]
| > > > - mv*@spam.guard. caspershouse.co m
| > > >
| > > > "Dennis C. Drumm" <de*******@prim acode.com> wrote in message
| > > > news:e4******** ******@TK2MSFTN GP12.phx.gbl...
| > > > > I have a class derived from a SortedList called SystemList that
| > contains
| > > a
| > > > > list of objects indexed with a string value.
| > > > >
| > > > > The definition of the objects contained in the SortedList have a
| > > boolean
| > > > > field and an event that is fired if the boolean field value
changes.
| > > > >
| > > > > The derived SortedList class has an override for the Add method.
The
| > Add
| > > > > method adds an event handler for the object's event for each new
| item
| > > > added
| > > > > to the list. In the program, the events work fine and all seems
| well.
| > > > >
| > > > > However, when I serialize and then try to deserialize the derived
| > > > > SortedList, deserialization fails. If I rem out the line that adds
| the
| > > > event
| > > > > handler in the Add method, everything serializes and deserializes
| jsut
| > > > fine.
| > > > > But with it in I can't deserialize.
| > > > >
| > > > > Can someone suggest what is going wrong here and how I can correct
| it?
| > > > >
| > > > > Thanks,
| > > > >
| > > > > Dennis
| > > > >
| > > > > BTW, the code for the deserialization is:
| > > > >
| > > > > BinaryFormatter bf = new BinaryFormatter ();
| > > > >
| > > > > bf.AssemblyForm at = FormatterAssemb lyStyle.Simple;
| > > > >
| > > > > IFormatter formatter = bf;
| > > > >
| > > > > Stream stream = new FileStream(name , FileMode.Open,
FileAccess.Read ,
| > > > > FileShare.Read) ;
| > > > >
| > > > > systemList = (SystemList)for matter.Deserial ize(stream);
| > > > >
| > > > > fileName = name; // update doc's file name
| > > > >
| > > > > stream.Close();
| > > > >
| > > > >
| > > >
| > > >
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #7
Thank you all! The problem has been resolved.

For any MS people that might be monitoring this thread, the VS.NET and MSDN
Library could use a little more content on how to add and remove event
handlers (and probably event handlers in general). There certainly is some
useful information in them, probably enough for some, but not enough for
anyone just getting familiar with the concept.

Thanks again,

Dennis
"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:en******** ******@TK2MSFTN GP11.phx.gbl...
Dennis,
Alternative to the event procedures as Nicholas showed, you can use the
field attribute modifier:

[field: NonSerialized]
public event EventHandler MyEvent;

Hope this helps
Jay

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
Nickolas:

I thought so, but trying the NonSerialize attribute does not work. That can
only be used on fields.

How do I remove/disconnect the event handlers? I would think -= but the
compiler doesn't like that.

Alternatively, is there a way to override the deserialization process and
add do something with the event handlers at that point?

Dennis

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote

in
message news:Or******** ******@tk2msftn gp13.phx.gbl...
Dennis,

When you have an event, you must store a reference to the target of
the
event. In other words, a reference to the object that holds the
method to be called when the event is fired is stored by the object firing the event.

Because of this, when you serialize the object that fires the events,
it
tries to serialize the event handlers as well.

To get around this, you will have to disconnect the event

handlers, or you could flag the event with the NonSerialized attribute (I am not
sure if
that works). Either way, you have to prevent the event handlers from

being
serialized.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dennis C. Drumm" <de*******@prim acode.com> wrote in message
news:e4******** ******@TK2MSFTN GP12.phx.gbl...
> I have a class derived from a SortedList called SystemList that

contains
a
> list of objects indexed with a string value.
>
> The definition of the objects contained in the SortedList have a

boolean
> field and an event that is fired if the boolean field value changes.
>
> The derived SortedList class has an override for the Add method. The

Add > method adds an event handler for the object's event for each new

item added
> to the list. In the program, the events work fine and all seems well. >
> However, when I serialize and then try to deserialize the derived
> SortedList, deserialization fails. If I rem out the line that adds the event
> handler in the Add method, everything serializes and deserializes jsut fine.
> But with it in I can't deserialize.
>
> Can someone suggest what is going wrong here and how I can correct it? >
> Thanks,
>
> Dennis
>
> BTW, the code for the deserialization is:
>
> BinaryFormatter bf = new BinaryFormatter ();
>
> bf.AssemblyForm at = FormatterAssemb lyStyle.Simple;
>
> IFormatter formatter = bf;
>
> Stream stream = new FileStream(name , FileMode.Open, FileAccess.Read ,
> FileShare.Read) ;
>
> systemList = (SystemList)for matter.Deserial ize(stream);
>
> fileName = name; // update doc's file name
>
> stream.Close();
>
>



Nov 15 '05 #8

Hi Dennis,

Thanks for your feedback.
I am glad your problem has been resolved.
The customer's suggestion will greatly improve our product.

I think you can provide your suggestion to:
http://register.microsoft.com/mswish/suggestion.asp
or email to: ms****@microsof t.com

Again, thanks for your information.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Dennis C. Drumm" <de*******@prim acode.com>
| From: "Dennis C. Drumm" <de*******@prim acode.com>
| References: <e4************ **@TK2MSFTNGP12 .phx.gbl>
<Or************ **@tk2msftngp13 .phx.gbl>
<Ov************ **@TK2MSFTNGP10 .phx.gbl>
<en************ **@TK2MSFTNGP11 .phx.gbl>
| Subject: Re: Serialization of SortedList objects when object contains
event
| Date: Thu, 16 Oct 2003 05:43:46 -0400
| Lines: 139
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <ev************ **@TK2MSFTNGP11 .phx.gbl>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: 206-15-137-92.dialup.zipli nk.net 206.15.137.92
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1917 48
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Thank you all! The problem has been resolved.
|
| For any MS people that might be monitoring this thread, the VS.NET and
MSDN
| Library could use a little more content on how to add and remove event
| handlers (and probably event handlers in general). There certainly is some
| useful information in them, probably enough for some, but not enough for
| anyone just getting familiar with the concept.
|
| Thanks again,
|
| Dennis
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in
message
| news:en******** ******@TK2MSFTN GP11.phx.gbl...
| > Dennis,
| > Alternative to the event procedures as Nicholas showed, you can use the
| > field attribute modifier:
| >
| > [field: NonSerialized]
| > public event EventHandler MyEvent;
| >
| > Hope this helps
| > Jay
| >
| > "Dennis C. Drumm" <de*******@prim acode.com> wrote in message
| > news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
| > > Nickolas:
| > >
| > > I thought so, but trying the NonSerialize attribute does not work.
That
| > can
| > > only be used on fields.
| > >
| > > How do I remove/disconnect the event handlers? I would think -= but
the
| > > compiler doesn't like that.
| > >
| > > Alternatively, is there a way to override the deserialization process
| and
| > > add do something with the event handlers at that point?
| > >
| > > Dennis
| > >
| > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om>
wrote
| > in
| > > message news:Or******** ******@tk2msftn gp13.phx.gbl...
| > > > Dennis,
| > > >
| > > > When you have an event, you must store a reference to the target
| of
| > > the
| > > > event. In other words, a reference to the object that holds the
| method
| > to
| > > > be called when the event is fired is stored by the object firing the
| > > event.
| > > >
| > > > Because of this, when you serialize the object that fires the
| > events,
| > > it
| > > > tries to serialize the event handlers as well.
| > > >
| > > > To get around this, you will have to disconnect the event
| handlers,
| > or
| > > > you could flag the event with the NonSerialized attribute (I am not
| sure
| > > if
| > > > that works). Either way, you have to prevent the event handlers
from
| > > being
| > > > serialized.
| > > >
| > > > Hope this helps.
| > > >
| > > >
| > > > --
| > > > - Nicholas Paldino [.NET/C# MVP]
| > > > - mv*@spam.guard. caspershouse.co m
| > > >
| > > > "Dennis C. Drumm" <de*******@prim acode.com> wrote in message
| > > > news:e4******** ******@TK2MSFTN GP12.phx.gbl...
| > > > > I have a class derived from a SortedList called SystemList that
| > contains
| > > a
| > > > > list of objects indexed with a string value.
| > > > >
| > > > > The definition of the objects contained in the SortedList have a
| > > boolean
| > > > > field and an event that is fired if the boolean field value
changes.
| > > > >
| > > > > The derived SortedList class has an override for the Add method.
The
| > Add
| > > > > method adds an event handler for the object's event for each new
| item
| > > > added
| > > > > to the list. In the program, the events work fine and all seems
| well.
| > > > >
| > > > > However, when I serialize and then try to deserialize the derived
| > > > > SortedList, deserialization fails. If I rem out the line that adds
| the
| > > > event
| > > > > handler in the Add method, everything serializes and deserializes
| jsut
| > > > fine.
| > > > > But with it in I can't deserialize.
| > > > >
| > > > > Can someone suggest what is going wrong here and how I can correct
| it?
| > > > >
| > > > > Thanks,
| > > > >
| > > > > Dennis
| > > > >
| > > > > BTW, the code for the deserialization is:
| > > > >
| > > > > BinaryFormatter bf = new BinaryFormatter ();
| > > > >
| > > > > bf.AssemblyForm at = FormatterAssemb lyStyle.Simple;
| > > > >
| > > > > IFormatter formatter = bf;
| > > > >
| > > > > Stream stream = new FileStream(name , FileMode.Open,
FileAccess.Read ,
| > > > > FileShare.Read) ;
| > > > >
| > > > > systemList = (SystemList)for matter.Deserial ize(stream);
| > > > >
| > > > > fileName = name; // update doc's file name
| > > > >
| > > > > stream.Close();
| > > > >
| > > > >
| > > >
| > > >
| > >
| > >
| >
| >
|
|
|

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1987
by: Casey | last post by:
So I'm using XmlSerializer to serialize out a wrapper object that contains an arbitrary number of other objects. The class definitions listed below are made to be very generic. Some of the objects contain other objects so when I declare my XmlSerializer I need to send along an array of types to be included in the serialization. That works fine until I run into 2 classes with the same name from different asseblies. This causes an XML...
5
2823
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would like it to return an XML serialized version of an object.
1
2816
by: Chris Bardon | last post by:
I'm working on a class library in .net, and am having a problem that I can only describe as a memory leak (which garbage collection was supposed to eliminate). My class library contains a single class, one member of which is a static SortedList of session data objects. One of my methods creates a new object on this list, and another removes the item from the list, which should make it eligible for garbage collection. I've tested this in...
5
7054
by: Rene | last post by:
I created a class (SomeClass), on that class, I declared a delegate (OnXyz) and then declared an event based on that delegate (Xyz). All this in the same class. After that, I created another class where I instantiate my previous class (SomeClass) and attach to its event like this: someObject. Xyz += new SomeClass. OnXyz (PrivateFunction); Life is good when I serialize the object but things go to hell when I deserialize it. What a hell?...
27
1944
by: Codemonkey | last post by:
Heya All, Sorry, but I think it's about time for a monkey-ramble. I've just had enough of trying to serialize even simple objects with VB. A simple task you may think - stick the <Serialized()> attribute on the class and away you go. As Homer would say - "D'Oh" The root of my problem lies in the way VB implements Events and the fact that you can't apply the <NonSerialized> attribute to the little rascals.
4
1471
by: Michael Rich | last post by:
Can anyone point me to a collection class that has serialization/deserialization with it - preferably in XML. I know this can't be done with a collection that inherits from CollectionBase, but I'm hoping someone has another collection class out there. Thanks; Michael
5
4000
by: Henry Jones | last post by:
I read a bit on Serialization and came up with the following definition: The System.Runtime.Serialization namespace contains classes that can be used for serializing and deserializing objects. Serialization is the process of converting an object or a graph of objects into a linear sequence of bytes for either storage or transmission to another location. Deserialization is the process of taking in stored information and recreating objects...
0
5928
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting problem serializing my stuff to XML when it comes to collections. I'm currently using .net2 with generic lists to prevent users putting all sorts of stuff in the arrays (Although im sure i'll be the only user of the classes but not the game, anyway)....
11
3683
by: William | last post by:
I'm looking for an example that would show how to serialize a c++ object at it's simplest w/o using any other api's. I have a class that I want to serialize and then pass to my obj-c class so I can send it over the wire. I'm just looking for how to serialize it, then pack it back up on the other end. Any help much appreciated.
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.