diff options
author | therzok <marius.ungureanu@xamarin.com> | 2019-05-31 15:35:35 +0300 |
---|---|---|
committer | therzok <marius.ungureanu@xamarin.com> | 2019-05-31 15:35:37 +0300 |
commit | bcd813c6070c406d733f70f1331ca8471190a85f (patch) | |
tree | b335423ff5c87680fb4d3ea27fc70ca95e57aa9f | |
parent | 7cd59b6bf79f94b22aa74af56bb41ca6b7825826 (diff) |
[Core] Remove redundant null check in SafeInvoke
The usual pattern is handler?.SafeInvoke(o, args). If we end up passing
a null, it's the same issue as with a null event handler, so prefer
not doing excessive checks that would not be expected of an extension
method.
Prefer not doing the null check in the handler, as it's redundant. By
the time SafeInvoke executes, EventHandler is a snapshot of the event
at that point in time.
The extension method invocation boils down to:
```
EventHandler eventHandler = this.m_myhandler;
if (eventHandler != null)
Extensions.Trigger(eventHandler, this, EventArgs.Empty);
```
And the add method of an event handler looks like so:
```
EventHandler eventHandler = this.m_myhandler;
EventHandler eventHandler2;
do
{
eventHandler2 = eventHandler;
EventHandler value2 = (EventHandler)Delegate.Combine(eventHandler2, value);
eventHandler = Interlocked.CompareExchange(ref this.m_myhandler, value2, eventHandler2);
} while ((object)eventHandler != eventHandler2);
```
So we're definitely going to never have a null inside SafeInvoke if it wasn't null
at capture site.
-rw-r--r-- | main/src/core/MonoDevelop.Core/CoreExtensions.cs | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/main/src/core/MonoDevelop.Core/CoreExtensions.cs b/main/src/core/MonoDevelop.Core/CoreExtensions.cs index 2cc04e9238..3d90766d6b 100644 --- a/main/src/core/MonoDevelop.Core/CoreExtensions.cs +++ b/main/src/core/MonoDevelop.Core/CoreExtensions.cs @@ -266,17 +266,14 @@ namespace System /// Invokes a callback catching and reporting exceptions thrown by handlers /// </summary> /// <typeparam name="T">Type of the event arguments</typeparam> - /// <param name="event">Event to invoke</param> + /// <param name="events">Event to invoke</param> /// <param name="sender">Sender of the event</param> /// <param name="args">Arguments of the event</param> - public static void SafeInvoke<T> (this EventHandler<T> @event, object sender, T args) + public static void SafeInvoke<T> (this EventHandler<T> events, object sender, T args) { - var events = @event; - if (events == null) - return; foreach (var ev in events.GetInvocationList ()) { try { - ((EventHandler < T > )ev) (sender, args); + ((EventHandler<T>)ev) (sender, args); } catch (Exception ex) { LoggingService.LogInternalError (ex); } @@ -286,14 +283,11 @@ namespace System /// <summary> /// Invokes a callback catching and reporting exceptions thrown by handlers /// </summary> - /// <param name="event">Event to invoke</param> + /// <param name="events">Event to invoke</param> /// <param name="sender">Sender of the event</param> /// <param name="args">Arguments of the event</param> - public static void SafeInvoke (this EventHandler @event, object sender, EventArgs args) + public static void SafeInvoke (this EventHandler events, object sender, EventArgs args) { - var events = @event; - if (events == null) - return; foreach (var ev in events.GetInvocationList ()) { try { ((EventHandler)ev) (sender, args); |