Skip to content

Commit

Permalink
Add Delegate.HasSingleTarget (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Dec 26, 2024
1 parent b8b1956 commit 64ffc64
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apiCount.include.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**API count: 433**
**API count: 434**
5 changes: 5 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
* `bool TryFormat(decimal, Span<char>, int, ReadOnlySpan<char>, 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<TKey, TValue>

* `bool Remove<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue) where TKey : notnull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)
Expand Down
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
**API count: 434**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->


**See [Milestones](../../milestones?state=closed) for release notes.**
Expand Down Expand Up @@ -537,6 +537,11 @@ The class `Polyfill` includes the following extension methods:
* `bool TryFormat(decimal, Span<char>, int, ReadOnlySpan<char>, 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<TKey, TValue>

* `bool Remove<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue) where TKey : notnull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)
Expand Down
26 changes: 26 additions & 0 deletions src/Polyfill/Polyfill_Delegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// <auto-generated />

using System.Runtime.CompilerServices;

#pragma warning disable

namespace Polyfills;

using System;
using System.Collections.Concurrent;

static partial class Polyfill
{
/// <summary>
/// Gets a value that indicates whether the Delegate has a single invocation target.
/// </summary>
//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
}
}
23 changes: 23 additions & 0 deletions src/Tests/PolyfillTests_Delegate.cs
Original file line number Diff line number Diff line change
@@ -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)
{
}
}

0 comments on commit 64ffc64

Please # to comment.