Home > Archive > C# > June 2004 > not wanting to serialize an event
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
not wanting to serialize an event
|
|
| alexis rzewski 2004-06-03, 7:31 pm |
| I have a design in place for a .NET app coded in C# in which the
business object drives the appearence of the UI and when the business
object experiences a change, it notifies the various UIs via events .
Problem is, when I tried serializing the business object to a file,
when traversing the tree of referenced objects, it found the event
targets, and tried to serialize my UI object (which is not
serializable). I then said to myself: "simple solution: mark that
event as non serializable" and went ahead to include the
NonSerializable attribute, but the compiler rejected that as saying
that such attribute applies only to "fields" (and I guess an event is
not a field).
Eventually I solved my problem by having a separate object declaring
the events and broadcasting the events, an object I called
EventNotifier, an instance of which is defined in my business object,
which forwards the requests to broadcast a change event to the
notifier object. In the code, I did mark this instance variable as
[NonSerializable], thus the EventNotifier and all of its target
objects (the UI classes) are never serialized.
Used SOAP serializer in C# 1.1.
It fixed the problem, but was wondering if others have better ideas.
| |
|
| Hi,
..Net probably wont let you mark the event as NonSerialized because
when you declare an event using 'Public event EventHandler MyEvent',
the event keyword makes the compiler do a little 'automagic' stuff.
Basically, it wraps the delegate in a property, which explains why it
wont let you use the 'field only' attribute. To achieve what you
want, a tidy way would be to explicitly declare a private member of
the event delegate type, and add public accessors, and a protected
virtual onEvent method to invoke it easily. This is a pretty well
documented design pattern (I think.. Can't seem to find where tho
:)), and it is particularly useful when debugging, as you can put
break points on events being subscribed to as well as invoked.
Here is a sample:
//Private member. Because this is a field, it will now
//accept the 'NonSerialized' attribute.
[NonSerialized]
private EventHandler _somethingHappened;
/// <summary>
/// Public accessor. When a class adds itself to this event,
/// this is used. Code could go here for debug, or to
/// limit the number of event clients, etc.
/// </summary>
public event EventHandler SomethingHappened
{
add
{
_somethingHappened += value;
}
remove
{
_somethingHappened -= value;
}
}
/// <summary>
/// Protected member, to allow this class, and sub classes,
/// access toraise and monitor the event.
/// </summary>
/// <remarks>
/// A lot of design patterns go a step further, by getting the
/// invocation list of _somethingHappened, manually calling each
target,
/// and removing those that failed. If this is done, be sure to
document
/// that exceptions in the client will result in the loss of
subscription
/// to the event.
/// </remarks>
/// <param name="e"></param>
protected virtual void OnSomethingHappened(EventArgs e)
{
if (_somethingHappened != null)
{
_somethingHappened(this, e);
}
}
Sorry for any line wrapping :$
hth -Mike
arzewski@hotmail.com (alexis rzewski) wrote in message news:<d209f52d.0405141020.3a582582@posting.google.com>...
> I have a design in place for a .NET app coded in C# in which the
> business object drives the appearence of the UI and when the business
> object experiences a change, it notifies the various UIs via events .
>
> Problem is, when I tried serializing the business object to a file,
> when traversing the tree of referenced objects, it found the event
> targets, and tried to serialize my UI object (which is not
> serializable). I then said to myself: "simple solution: mark that
> event as non serializable" and went ahead to include the
> NonSerializable attribute, but the compiler rejected that as saying
> that such attribute applies only to "fields" (and I guess an event is
> not a field).
>
> Eventually I solved my problem by having a separate object declaring
> the events and broadcasting the events, an object I called
> EventNotifier, an instance of which is defined in my business object,
> which forwards the requests to broadcast a change event to the
> notifier object. In the code, I did mark this instance variable as
> [NonSerializable], thus the EventNotifier and all of its target
> objects (the UI classes) are never serialized.
>
> Used SOAP serializer in C# 1.1.
>
> It fixed the problem, but was wondering if others have better ideas.
|
|
|
|
|