views:

43

answers:

1

Let's say I have a "Processor" interface exposing an event - OnProcess. Usually the implementors do processing thus I can safely subscribe on this event and be sure it will be fired. But one processor doesn't do processing - thus I want to prevent subscribers to subscibe on it. Can I do that? In other words in the code below I want the last line to throw an exception:

var emptyProcessor = new EmptyProcessor();
emptyProcessor.OnProcess += event_handler; // this line should throw an exception

Thanks in advance.

+6  A: 
class EmptyProcessor {

    public override event EventHandler OnProcess { 
        add { throw new Exception(" :( "); }
        remove { }
    }
}
Jimmy
Thank you so much
Moshe
Wow... I didn't even know you could do that. I just wrote a quick sample application to test it, and who knew... it worked. Very interesting.
Nick
+1, although I'm not sure this exception message would be very helpful ;)
Thomas Levesque