In C #, can you set event limits?

I want to limit the types that can subscribe to a specific event based on the interface that they implement.

those. I want the Employee object to implement IPerson to subscribe to the event handler, but another Truck object that implements a completely different interface, which should be limited. Any object that implements the correct method template can sign out of the box.

How to limit it?

I ask a question: I am implementing an Observer pattern, but am trying to do this using C # events. I have an example similar to what MSDN is here .

My concern, although there is still ANY object with ANY structure (provided that it contains the correct delegate method), can be executed when the event fires. Therefore, in my example above, Employee can implement a method that makes sense for objects in my example, but then anyone could make another class - the "truck" in my example above, with any structure (if it implements the method again) and bind to the thematic the event ... It's definitely more worried about the use of objects and good design, and maybe I'm picky here, but it bothers me.

+7
source share
3 answers

In C # can I set event limits?

Nope.

I want to limit the types that can subscribe to a specific event based on the interface that they implement.

Event listeners are instances of delegates of a type compatible with the type of event. "Types" are not event listeners.

those. I want the Employee object to implement IPerson to subscribe to the event handler, but another Truck object that implements a completely different interface, which should be limited.

The employee object does not listen to the event in the first place. The only thing that listens to the event is the delegate object.

You say you want to accept delegates who are delegates of methods that are instances of a method of a particular class? This is very strange, and I recommend not trying to do it .

It is impossible to prevent this at compile time, but if you really delve into this strange thing, you can do it at runtime.

To do this, like the other answers, you can create access methods for the adder and deletion for the event, and then put a check in the adder that checks that the delegate recipient matches the type that you approve, and throw an exception if it is not.

But again, this sounds like an unusually bad idea. It is up to the listener to decide whether they are interested in the event, and not to the source of the events, in order to check his listeners. Users have a reasonable expectation that any available event can be listened to by any delegate of a compatible type.

Perhaps you can explain why you want to do this strange thing; may be the best way to achieve your goal.

+7
source

This is a really bad idea. Please, do not do that.

What are you trying to protect against in reality? Any "hostile" type can implement an interface (and if they cannot see the interface because it is internal to your project, then there is no reason not to make it an internal event as well), so your blacklist is not so well respected. To make matters worse, this complicates the types in the white list โ€” they wonโ€™t be able to delegate to listeners examples of โ€œcloseโ€ related types. Even using a closure (using the lambda / anonymity method) can suddenly break subscriptions - the โ€œviewโ€ subscription object can become an instance of the class generated by the compiler (blacklist).

This is a bad security mechanism, and it does not provide security at compile time. It does not do a good job of saving the bad guys and makes life difficult for the good guys.

In any case, this is possible - you will need to use a custom implementation for the event and check each subscription request.

//Please don't do this. private EventHandler myEventField = delegate { }; // Add synchronization if required. public event EventHandler MyEvent { add { if(value.Target is IPerson) myEventField += value; else throw new ArgumentException("Subscriber must implement IPerson", "value"); } remove { myEventField -= value; } } private void RaiseMyEvent() { myEventField(this, EventArgs.Empty); } 
+8
source

I canโ€™t imagine why you need it, it looks a bit messy at first glance, but in any case, you can encapsulate this logic of restrictions in an event holder and set Subscribe(delegate handler) to subscribe to the event, and not to publish the public event itself

 class EventHolder { private event EventHandler<NewUpdateEventArgs> NewUpdate; public void SubscribeForNewUpdates(object subscriptionOwner, Action<NewUpdateEventArgs> callback) { if (subscriptionOwner.GetType() == ... or subscriptionOwner is ...) { this.NewUpdate += .. subscribe callback } } } 
+1
source

All Articles