Remove events without knowing their names - c#

Is it possible to instead of doing this:
person.Walking -= person_Walking1;
person.Walking -= person_Walking2;
person.Walking -= person_Walking3;
Do this:
person.Walking = // remove all the handlers without knowing their names
Thanks.

No. Part of the point of events is that they prevent you from doing that. If I've subscribed to a button click and you've subscribed to a button click, what right have you to remove my handler? (Okay, that's anthropomorphising somewhat, but you get the idea.) Note that it's not a matter of knowing the "name" of an event handler - you have to be able to provide a reference to an "equal" delegate instance.
For example, if you subscribe to an event using an anonymous method or a lambda expression, you'll have to keep a reference to that somewhere:
EventHandler handler = (sender, args) => Console.WriteLine("Clicked!");
button.Click += handler;
...
button.Click -= handler;
When you use the name of a method, that's performing a method group conversion from the name of the method to a delegate instance:
button.Click += HandleEvent;
...
button.Click -= HandleEvent;
Here there are two separate delegate instances involved, but they're equal as they have the same invocation list (they do the same thing) and they have the same target (they're doing that thing "on" the same object).
EDIT: I'm assuming you only have access to it as an event, not as a field - if you're writing code in the class which publishes the event, you can do what you like, and setting the field to null (or removing it from the collection, or however your implementation works) is fine.

Sure. Just set:
person.Walking = null;

This is one of the reasons events were invented. If you wanted to do something like that, use delegates.

Not from outside the class in which the events are defined. If you really wanted to, you could define a method like this:
public void ClearEventListeners() {
MyEvent.Clear();
}
(The exact call may be different, if it exists, but IntelliSense should point you in the right direction.)

Related

c# change modifier when creating event method with +=

If I subscribe a new Method to a Handler and Press "Tab" twice after the "+=" VS will implement a Body like:
public class A {
public A(){
button1.Click += OnButton1Click();
}
private OnButton1Click(object sender, EventArgs e){
}
}
How can I change VS to create the Body of the Method as public instead of private?
Kind regards,
Asat0r
How can I change VS to create the Body of the Method as public instead
of private?
Not sure if there is any option but why you would like to do that? event handlers are not meant to be public. If you want to call the logic inside handler from some other type then refactor that logic to a helper method and re-use that instead probably.
Other answers have suggested that the method shouldn't be public.
Broadly speaking, I reject that - at least without further justification. If you want to expose a method with exactly that signature, I'd normally go ahead and do so. Two caveats around that though:
Normally the signature of an event handler isn't useful except as an event handler; the parameters aren't generally useful when calling the method directly. There are exceptions to that as ever.
If you subscribe an event handler with a public method, any other code can unsubscribe that event handler by creating an equal delegate. If you control all the code in the application, that's not much of a problem.
Both cases can be solved by exposing a public method with exactly the parameters you want (which could still be event handler parameters, but often won't be) and then using a lambda expression to subscribe to the event:
// I want to be able to call this from other code
public void SaveItem()
{
// ...
}
// Hook up the event handler using a lambda expression
saveButton.Click += (sender, args) => SaveItem();
On the other hand, if neither of the bullet points are an issue, go ahead and make the method public manually. I wouldn't expect there to be a VS shortcut to do so, but simply specifying the public modifier is pretty simple.

Proper way to reuse delegate events in c#

I am trying to see if I can use delegate events and reuse them so that I do not have to make multiple click events. Right now I have the following...
namespace EventsWPF
{
public partial class MainWindow : Window
{
public delegate void MyEvent(object sender, RoutedEventArgs e);
public MainWindow()
{
InitializeComponent();
btn1.Click += MyEvent;
btn2.Click += MyEvent;
}
}
}
Is this the correct way to do it? Or am I thinking about this the wrong way? I know you can use Lambda expressions as an event handler. But if I have multiple events I don't want to create multiple Lamda expressions for each handler when I can just reuse a delegate.
MyEvent is unnecessary here as you are not creating your own event, just subscribing to someone else's. The delegate keyword declares a method signature and return type - it does not create instances or define method bodies.
Here is how you re-use a lambda expression:
Action<object, RoutedEventArgs> myHandler = (o, e) =>
{
/* write event handler code here, you must */
};
btn1.Click += myHandler;
btn2.Click += myHandler;
Yes, if two buttons should have the same exact logic run; then attaching the same handler is perfectly fine. You won't be able to do it by attaching a delegate type though; you'll need an actual method (as described by #hoodaticus).
If this is in WPF:
Usually however, two buttons don't do the same thing and you can't get away with this. Far more commonly, two buttons do the same thing but with different arguments. Unfortunately, a WinForms style approach will be very painful here (you can check against sender but.... ewww).
In that case (actually, in all cases) you really want to take advantage of the MVVM pattern and set up commands for your buttons (not click handlers). Then you can use CommandParameter to get the custom piece of data into the handler.

Check if a specific event handler method already attached

Related to this question,
Check if an event already exists
but the difference is I just want to know if a particular method is attached to the event. So there may be other methods attached, but I just want to know if a particular one exists.
My environment is C# in dotnet 4.0.
E.g.
Event += MyMethod1;
Event += MyMethod2;
// Some code
if (MyMethod1IsAttachedToEvent())
{
// Achieved goal
}
Is this possible?
No. You cannot.
The event keyword was explicitly invented to prevent you from doing what you want to do. It makes the delegate object for the event inaccessible so nobody can mess with the events handlers.
Source : How to dermine if an event is already subscribed
Late answer here. I believe Parimal Raj answer is correct, as I could not find a way to directly access the events. However, here are two methods I created to get around this:
Delete before adding. If the method isn't there, I did not receive an error trying to delete the nonexistant method. This way you can insure the invocation list calls method1 only once.
Event -= MyMethod1;
Event += MyMethod1;
The objects you are adding an event to may have a .Tag property. You can use the Tag to store info about the methods you already added. Here I only have one method, so I just need to check if Tag is null. But more complicated scenarios can be handled this way:
if(control.Tag == null)
{
//ony added once, when tag is null
control.TextChanged += new EventHandler(validate);
control.Tag = new ControlTag();
}
Event.GetInvocationList().Any(x => x.Method.Name.Equals("yourmethodname"));
foreach ( Delegate existingHandler in this.EventHandler.GetInvocationList() )
{
if ( existingHandler == prospectiveHandler )
{
return true;
}
}
loop through the delegates using the GetInvocationList method.

How can I check if an event has been subscribed to, in .NET?

At one point in my code, i subscribe to the following event :-
UploadFolderMessageQueue.ReceiveCompleted += UploadMSMQReceiveCompleted;
works great and when the Message Queue's Recieved Completed event fires, my delegate handles it.
Now, I'm wanting to CHECK to see if the event has been subscribed to, before I subscribe to it. I get an compile time error when I do :-
// Compile Time Errors...
if (UploadFolderMessageQueue.ReceiveCompleted == null)
{
UploadFolderMessageQueue.ReceiveCompleted += UploadMSMQReceiveCompleted;
UploadFolderMessageQueue.Formatter =
new XmlMessageFormatter(new[] {typeof (string)});
}
The event
'System.Messaging.MessageQueue.ReceiveCompleted'
can only appear on the left hand side
of += or -=
I know this is embarrassingly simple .. but I'm stumped :( Any suggestions?
If you need to make sure that there is only one subscriber you can use the following code:
UploadFolderMessageQueue.ReceiveCompleted -= UploadMSMQReceiveCompleted;
UploadFolderMessageQueue.ReceiveCompleted += UploadMSMQReceiveCompleted;
If UploadFolderMessageQueue.ReceiveCompleted is null then the first line will do nothing, in other case the event handler will be removed. That means UploadFolderMessageQueue.ReceiveCompleted will always have only one subscriber (of course if the UploadMSMQReceiveCompleted is the only one event handler for that event).
you cannot do this from subscriber to the event. Only publisher can check if there are any subscribers. You will need to keep track of subscription using some other mechanism in your class like:
UploadFolderMessageQueue.ReceiveCompleted += UploadMSMQReceiveCompleted;
bool handlerAttached=true;
then you can use this:
if(handlerAttached)
{
//DO YOUR STUFF
}
The null test can only be performed within the class that declares the event (i.e. the UploadFolderMessageQueue type.)
1) If you have access to the source of this class you can add a method or property that does the test and returns a boolean result that you can check before subscribing.
2) If you cannot modify the declaring class, and you are only checking for re-subscriptions from your own code, you can separately record the subscription in a boolean variable, and then check that variable before attempting to (re) subscribe.
3) If you cannot change the declaring class and you are checking for subscriptions from code other than your own, there doesn't appear to be a solution.

Has an event handler already been added?

Is there a way to tell if an event handler has been added to an object? I'm serializing a list of objects into/out of session state so we can use SQL based session state... When an object in the list has a property changed it needs to be flagged, which the event handler took care of properly before. However now when the objects are deserialized it isn't getting the event handler.
In an fit of mild annoyance, I just added the event handler to the Get property that accesses the object. It's getting called now which is great, except that it's getting called like 5 times so I think the handler just keeps getting added every time the object is accessed.
It's really safe enough to just ignore, but I'd rather make it that much cleaner by checking to see if the handler has already been added so I only do so once.
Is that possible?
EDIT: I don't necessarily have full control of what event handlers are added, so just checking for null isn't good enough.
I recently came to a similar situation where I needed to register a handler for an event only once. I found that you can safely unregister first, and then register again, even if the handler is not registered at all:
myClass.MyEvent -= MyHandler;
myClass.MyEvent += MyHandler;
Note that doing this every time you register your handler will ensure that your handler is registered only once.
Sounds like a pretty good practice to me :)
From outside the defining class, as #Telos mentions, you can only use EventHandler on the left-hand side of a += or a -=. So, if you have the ability to modify the defining class, you could provide a method to perform the check by checking if the event handler is null - if so, then no event handler has been added. If not, then maybe and you can loop through the values in
Delegate.GetInvocationList. If one is equal to the delegate that you want to add as event handler, then you know it's there.
public bool IsEventHandlerRegistered(Delegate prospectiveHandler)
{
if ( this.EventHandler != null )
{
foreach ( Delegate existingHandler in this.EventHandler.GetInvocationList() )
{
if ( existingHandler == prospectiveHandler )
{
return true;
}
}
}
return false;
}
And this could easily be modified to become "add the handler if it's not there". If you don't have access to the innards of the class that's exposing the event, you may need to explore -= and +=, as suggested by #Lou Franco.
However, you may be better off reexamining the way you're commissioning and decommissioning these objects, to see if you can't find a way to track this information yourself.
If this is the only handler, you can check to see if the event is null, if it isn't, the handler has been added.
I think you can safely call -= on the event with your handler even if it's not added (if not, you could catch it) -- to make sure it isn't in there before adding.
This example shows how to use the method GetInvocationList() to retrieve delegates to all the handlers that have been added. If you are looking to see if a specific handler (function) has been added then you can use array.
public class MyClass
{
event Action MyEvent;
}
...
MyClass myClass = new MyClass();
myClass.MyEvent += SomeFunction;
...
Action[] handlers = myClass.MyEvent.GetInvocationList(); //this will be an array of 1 in this example
Console.WriteLine(handlers[0].Method.Name);//prints the name of the method
You can examine various properties on the Method property of the delegate to see if a specific function has been added.
If you are looking to see if there is just one attached, you can just test for null.
The only way that worked for me is creating a Boolean variable that I set to true when I add the event. Then I ask: If the variable is false, I add the event.
bool alreadyAdded = false;
This variable can be global.
if(!alreadyAdded)
{
myClass.MyEvent += MyHandler;
alreadyAdded = true;
}
If I understand your problem correctly you may have bigger issues. You said that other objects may subscribe to these events. When the object is serialized and deserialized the other objects (the ones that you don't have control of) will lose their event handlers.
If you're not worried about that then keeping a reference to your event handler should be good enough. If you are worried about the side-effects of other objects losing their event handlers, then you may want to rethink your caching strategy.
i agree with alf's answer,but little modification to it is,,
to use,
try
{
control_name.Click -= event_Click;
main_browser.Document.Click += Document_Click;
}
catch(Exception exce)
{
main_browser.Document.Click += Document_Click;
}
EventHandler.GetInvocationList().Length > 0

Categories