Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Apr 22, 2024
2 parents 76035b3 + a626d59 commit e66f13b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 20 deletions.
2 changes: 1 addition & 1 deletion apiCount.include.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**API count: 284**
**API count: 285**
19 changes: 10 additions & 9 deletions api_list.include.md

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions readme.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class Consume
IDictionary<int, int> idictionary = new Dictionary<int, int>();
idictionary.AsReadOnly();

typeof(List<string>).IsAssignableTo(typeof(string));
typeof(List<string>).IsAssignableTo(null);

var enumerable = (IEnumerable<string>) new List<string>
{
"a",
Expand Down
45 changes: 45 additions & 0 deletions src/Polyfill/GuidPolyfill.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma warning disable

// ReSharper disable RedundantUsingDirective
// ReSharper disable PartialTypeWithSinglePart

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using Link = System.ComponentModel.DescriptionAttribute;

[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
#if PolyPublic
public
#endif
static partial class GuidPolyfill
{
/// <summary>
/// Tries to parse a string into a value.
/// </summary>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.guid.tryparse#system-guid-tryparse(system-string-system-iformatprovider-system-guid@)")]
public static bool TryParse(string? target, IFormatProvider? provider, out Guid result) =>
#if !NET7_0_OR_GREATER
Guid.TryParse(target, out result);
#else
Guid.TryParse(target, provider, out result);
#endif

#if FeatureMemory
/// <summary>
/// Tries to parse a span of UTF-8 characters into a value.
/// </summary>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryparse#system-byte-tryparse(system-readonlyspan((system-byte))-system-iformatprovider-system-byte@)")]
public static bool TryParse(ReadOnlySpan<byte> target, IFormatProvider? provider, out byte result) =>
#if !NET8_0_OR_GREATER
byte.TryParse(Encoding.UTF8.GetString(target.ToArray()), NumberStyles.Integer, provider, out result);
#else
byte.TryParse(target, provider, out result);
#endif

#endif
}
10 changes: 10 additions & 0 deletions src/Polyfill/Polyfill_Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma warning disable

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Link = System.ComponentModel.DescriptionAttribute;

Expand All @@ -29,6 +30,15 @@ public static bool IsGenericMethodParameter(this Type target)
#endif
}

#if NETFRAMEWORK || NETSTANDARD || NETCOREAPPX
/// <summary>
/// Determines whether the current type can be assigned to a variable of the specified targetType.
/// </summary>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.type.isassignableto")]
public static bool IsAssignableTo(this Type target, [NotNullWhen(true)] Type? targetType) =>
targetType?.IsAssignableFrom(target) ?? false;
#endif

#if !NET6_0_OR_GREATER

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Tests/PolyfillTests_Type.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
partial class PolyfillTests
{
[Test]
public void IsAssignableTo()
{
Assert.True(typeof(List<string>).IsAssignableTo(typeof(IList)));
Assert.False(typeof(List<string>).IsAssignableTo(typeof(string)));
Assert.False(typeof(List<string>).IsAssignableTo(null));
}
}

0 comments on commit e66f13b

Please sign in to comment.