From 64ffc64a0d487e9cc7848a723cd40ffc8481d81c Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 26 Dec 2024 23:21:23 +1100 Subject: [PATCH] Add Delegate.HasSingleTarget (#270) --- apiCount.include.md | 2 +- api_list.include.md | 5 +++++ readme.md | 7 ++++++- src/Polyfill/Polyfill_Delegate.cs | 26 ++++++++++++++++++++++++++ src/Tests/PolyfillTests_Delegate.cs | 23 +++++++++++++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/Polyfill/Polyfill_Delegate.cs create mode 100644 src/Tests/PolyfillTests_Delegate.cs diff --git a/apiCount.include.md b/apiCount.include.md index 155e0e8..daf25ed 100644 --- a/apiCount.include.md +++ b/apiCount.include.md @@ -1 +1 @@ -**API count: 433** \ No newline at end of file +**API count: 434** \ No newline at end of file diff --git a/api_list.include.md b/api_list.include.md index 8c1e9d4..ac830ce 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -68,6 +68,11 @@ * `bool TryFormat(decimal, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat#system-decimal-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) +#### Delegate + + * `bool HasSingleTarget(Delegate)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.delegate.hassingletarget) + + #### Dictionary * `bool Remove(Dictionary, TKey, TValue) where TKey : notnull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove) diff --git a/readme.md b/readme.md index 45fe5a6..f38179d 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru * `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0` -**API count: 433** +**API count: 434** **See [Milestones](../../milestones?state=closed) for release notes.** @@ -537,6 +537,11 @@ The class `Polyfill` includes the following extension methods: * `bool TryFormat(decimal, Span, int, ReadOnlySpan, IFormatProvider?)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat#system-decimal-tryformat(system-span((system-char))-system-int32@-system-readonlyspan((system-char))-system-iformatprovider)) +#### Delegate + + * `bool HasSingleTarget(Delegate)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.delegate.hassingletarget) + + #### Dictionary * `bool Remove(Dictionary, TKey, TValue) where TKey : notnull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove) diff --git a/src/Polyfill/Polyfill_Delegate.cs b/src/Polyfill/Polyfill_Delegate.cs new file mode 100644 index 0000000..048c2a0 --- /dev/null +++ b/src/Polyfill/Polyfill_Delegate.cs @@ -0,0 +1,26 @@ +// + +using System.Runtime.CompilerServices; + +#pragma warning disable + +namespace Polyfills; + +using System; +using System.Collections.Concurrent; + +static partial class Polyfill +{ + /// + /// Gets a value that indicates whether the Delegate has a single invocation target. + /// + //Link: https://learn.microsoft.com/en-us/dotnet/api/system.delegate.hassingletarget + public static bool HasSingleTarget(this Delegate target) + { +#if NET9_0_OR_GREATER + return target.HasSingleTarget; +#else + return target.GetInvocationList().Length == 1; +#endif + } +} diff --git a/src/Tests/PolyfillTests_Delegate.cs b/src/Tests/PolyfillTests_Delegate.cs new file mode 100644 index 0000000..55fa753 --- /dev/null +++ b/src/Tests/PolyfillTests_Delegate.cs @@ -0,0 +1,23 @@ +partial class PolyfillTests +{ + public static event EventHandler? MyEvent; + + [Test] + public void HasSingleTarget() + { + MyEvent += Handler; + Assert.IsTrue(MyEvent.HasSingleTarget()); + MyEvent += Handler; + Assert.IsFalse(MyEvent.HasSingleTarget()); + var action = () => + { + }; + Assert.IsTrue(action.HasSingleTarget()); + action += action; + Assert.IsFalse(action.HasSingleTarget()); + } + + static void Handler(object? sender, EventArgs e) + { + } +} \ No newline at end of file