Event Subscribers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a module that acts as a publisher of events. The clients subscribe
for the events using the '+=' operator. Instead, I would like the clients to
call a method like "RegisterForXEvent" passing the required information.
Inside that method, I would like to add the client to the list of
subscribers. Is it possible to implement the above. If so, how do I do it?
What kinda information should the client pass to the Event publisher?
 
Well, technically yes it would be possible, by making the RegisterForXEvent
method accept a delegate of the appropriate type... but the big question
would be "why?". The standard event mechanism is well understood, and
(frankly) works very well in most scenarios.

If (inside the standard event model) you want to do something a bit more
bespoke when registering, you can do this by providing your own "add" and
"remove" implementations (that are the += and -= operators):

private event EventHandler _myEvent;
public event EventHandler MyEvent {
add {
if (SomeTest()) // check if the subscriber is permitted
throw new Exception(@"Talk to the hand, 'cos the face
ain't listening");
_myEvent += value;
}
remove { _myEvent -= value; }
}
protected void OnMyEvent() {
if (_myEvent != null) _myEvent(this, new EventArgs());
}

If you are trying to do something more complicated, then there isn't enough
information in your post to answer the "what kinda information" question.

Marc
 
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.
 
Fair enough answer...

I'm not too familiar with hooking up COM (or other unmanaged) clients to
..Net events (or some similar solution), so now that we all properly
understand the question, I'll duck out (I don't want to confuse things).

Marc
 
Tim said:
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.

If you mean that you client is the actual object that the method the
delegate represenst is called on, you can get that from the delegate
using its Target property.
HTH,
Andy
 
Tim said:
Hi,

Thanks for the response - Marc. Regarding the question "Why" - The
clients could be COM/PInvoke clients or managed clients. Instead of
describing how they need to subscribe to the events, it would be much simpler
to ask them to make a method call. Well, I am not sure about the complexities
that may be involved in the process - which I am just trying to figure out.

Regarding the 'Add' implementation, what information does the caller
have to pass to the Event publisher? Will that suit my requirement - of
having to support diff kinds of clients.
If you mean that you client is the actual object that the method the
delegate represenst is called on, you can get that from the delegate
using its Target property.
HTH,
Andy
 
Hi,

Actually, I am interested in knowing if there is a way for Clients to
subscribe for events by making a method call. If so, what is the information
that needs to be provided - i.e. the signature of the method that should be
exposed by the event publisher.
 
Back
Top