From c8edac0f1452a7425c052a87adadadd2a73546de Mon Sep 17 00:00:00 2001 From: Drew Tingen Date: Mon, 3 Aug 2020 16:48:16 -0400 Subject: [PATCH] feat: Add extensions to raise events for common event args --- CHANGELOG.md | 1 + .../EventArguments/BoolEventArgs.cs | 20 ++++++++++++++++++- .../EventArguments/CharEventArgs.cs | 18 ++++++++++++++++- .../EventArguments/DateTimeEventArgs.cs | 16 +++++++++++++++ .../DateTimeNullableEventArgs.cs | 16 +++++++++++++++ .../EventArguments/FloatEventArgs.cs | 20 ++++++++++++++++++- .../EventArguments/GenericEventArgs.cs | 16 +++++++++++++++ .../EventArguments/IntEventArgs.cs | 20 ++++++++++++++++++- .../EventArguments/StringEventArgs.cs | 20 ++++++++++++++++++- .../EventArguments/UShortEventArgs.cs | 18 ++++++++++++++++- .../EventArguments/XmlRecursionEventArgs.cs | 16 +++++++++++++++ 11 files changed, 175 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 633af8b..3ec51fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added Get and Set extensions to PropertyInfo in SIMPLSHARP to mimic overloads avaliable in NETSTANDARD - Added Collection extensions for setting and adding ranges of items - Added a method for getting the total number of seconds in a date + - Added extensions to raise events with common event args using the data directly ### Changed - Repeater changed to use configured callbacks instead of a dumb event diff --git a/ICD.Common.Utils/EventArguments/BoolEventArgs.cs b/ICD.Common.Utils/EventArguments/BoolEventArgs.cs index c73580c..f0a38d3 100644 --- a/ICD.Common.Utils/EventArguments/BoolEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/BoolEventArgs.cs @@ -1,4 +1,8 @@ -namespace ICD.Common.Utils.EventArguments +using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; + +namespace ICD.Common.Utils.EventArguments { public sealed class BoolEventArgs : GenericEventArgs { @@ -20,4 +24,18 @@ { } } + + public static class BoolEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler extends, object sender, bool data) + { + extends.Raise(sender, new BoolEventArgs(data)); + } + } } diff --git a/ICD.Common.Utils/EventArguments/CharEventArgs.cs b/ICD.Common.Utils/EventArguments/CharEventArgs.cs index c83509b..fa702e8 100644 --- a/ICD.Common.Utils/EventArguments/CharEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/CharEventArgs.cs @@ -1,4 +1,6 @@ -using ICD.Common.Properties; +using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils.EventArguments { @@ -9,4 +11,18 @@ namespace ICD.Common.Utils.EventArguments { } } + + public static class CharEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler extends, object sender, char data) + { + extends.Raise(sender, new CharEventArgs(data)); + } + } } diff --git a/ICD.Common.Utils/EventArguments/DateTimeEventArgs.cs b/ICD.Common.Utils/EventArguments/DateTimeEventArgs.cs index 6d5f60f..5ef7686 100644 --- a/ICD.Common.Utils/EventArguments/DateTimeEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/DateTimeEventArgs.cs @@ -1,4 +1,6 @@ using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils.EventArguments { @@ -13,4 +15,18 @@ namespace ICD.Common.Utils.EventArguments { } } + + public static class DateTimeEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler extends, object sender, DateTime data) + { + extends.Raise(sender, new DateTimeEventArgs(data)); + } + } } diff --git a/ICD.Common.Utils/EventArguments/DateTimeNullableEventArgs.cs b/ICD.Common.Utils/EventArguments/DateTimeNullableEventArgs.cs index a2519ac..acd5656 100644 --- a/ICD.Common.Utils/EventArguments/DateTimeNullableEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/DateTimeNullableEventArgs.cs @@ -1,4 +1,6 @@ using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils.EventArguments { @@ -12,4 +14,18 @@ namespace ICD.Common.Utils.EventArguments { } } + + public static class DateTimeNullableEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler extends, object sender, DateTime? data) + { + extends.Raise(sender, new DateTimeNullableEventArgs(data)); + } + } } diff --git a/ICD.Common.Utils/EventArguments/FloatEventArgs.cs b/ICD.Common.Utils/EventArguments/FloatEventArgs.cs index c7b3063..704e43b 100644 --- a/ICD.Common.Utils/EventArguments/FloatEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/FloatEventArgs.cs @@ -1,4 +1,8 @@ -namespace ICD.Common.Utils.EventArguments +using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; + +namespace ICD.Common.Utils.EventArguments { public sealed class FloatEventArgs : GenericEventArgs { @@ -11,4 +15,18 @@ { } } + + public static class FloatEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler extends, object sender, float data) + { + extends.Raise(sender, new FloatEventArgs(data)); + } + } } diff --git a/ICD.Common.Utils/EventArguments/GenericEventArgs.cs b/ICD.Common.Utils/EventArguments/GenericEventArgs.cs index 51f40cd..5547864 100644 --- a/ICD.Common.Utils/EventArguments/GenericEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/GenericEventArgs.cs @@ -1,4 +1,6 @@ using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils.EventArguments { @@ -25,4 +27,18 @@ namespace ICD.Common.Utils.EventArguments m_Data = data; } } + + public static class GenericEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler> extends, object sender, T data) + { + extends.Raise(sender, new GenericEventArgs(data)); + } + } } diff --git a/ICD.Common.Utils/EventArguments/IntEventArgs.cs b/ICD.Common.Utils/EventArguments/IntEventArgs.cs index 84a3537..5f9def1 100644 --- a/ICD.Common.Utils/EventArguments/IntEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/IntEventArgs.cs @@ -1,4 +1,8 @@ -namespace ICD.Common.Utils.EventArguments +using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; + +namespace ICD.Common.Utils.EventArguments { public sealed class IntEventArgs : GenericEventArgs { @@ -10,4 +14,18 @@ { } } + + public static class IntEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler extends, object sender, int data) + { + extends.Raise(sender, new IntEventArgs(data)); + } + } } diff --git a/ICD.Common.Utils/EventArguments/StringEventArgs.cs b/ICD.Common.Utils/EventArguments/StringEventArgs.cs index db25463..0de5d40 100644 --- a/ICD.Common.Utils/EventArguments/StringEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/StringEventArgs.cs @@ -1,4 +1,8 @@ -namespace ICD.Common.Utils.EventArguments +using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; + +namespace ICD.Common.Utils.EventArguments { public sealed class StringEventArgs : GenericEventArgs { @@ -19,4 +23,18 @@ { } } + + public static class StringEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler extends, object sender, string data) + { + extends.Raise(sender, new StringEventArgs(data)); + } + } } diff --git a/ICD.Common.Utils/EventArguments/UShortEventArgs.cs b/ICD.Common.Utils/EventArguments/UShortEventArgs.cs index f1abe8a..1a4e70a 100644 --- a/ICD.Common.Utils/EventArguments/UShortEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/UShortEventArgs.cs @@ -1,4 +1,6 @@ -using ICD.Common.Properties; +using System; +using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils.EventArguments { @@ -13,4 +15,18 @@ namespace ICD.Common.Utils.EventArguments { } } + + public static class UShortEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler extends, object sender, ushort data) + { + extends.Raise(sender, new UShortEventArgs(data)); + } + } } diff --git a/ICD.Common.Utils/EventArguments/XmlRecursionEventArgs.cs b/ICD.Common.Utils/EventArguments/XmlRecursionEventArgs.cs index c6cfa98..37f569f 100644 --- a/ICD.Common.Utils/EventArguments/XmlRecursionEventArgs.cs +++ b/ICD.Common.Utils/EventArguments/XmlRecursionEventArgs.cs @@ -1,5 +1,6 @@ using System; using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils.EventArguments { @@ -26,4 +27,19 @@ namespace ICD.Common.Utils.EventArguments #endregion } + + public static class XmlRecursionEventArgsExtensions + { + /// + /// Raises the event safely. Simply skips if the handler is null. + /// + /// + /// + /// + /// + public static void Raise([CanBeNull]this EventHandler extends, object sender, string outer, string[] path) + { + extends.Raise(sender, new XmlRecursionEventArgs(outer, path)); + } + } }