Skip to content

Commit

Permalink
Add polyfill for Type.IsAssignableTo
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Mar 21, 2024
1 parent c27d4c5 commit f21cca5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Diagnostics.CodeAnalysis;

static partial class PolyfillExtensions
{
public static bool IsAssignableTo(this Type target, [NotNullWhen(true)] Type? targetType)
{
return targetType?.IsAssignableFrom(target) ?? false;
}
}
8 changes: 8 additions & 0 deletions Meziantou.Polyfill.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,14 @@ public void Enumerable_Index()
{
Assert.Equal([(0, "a"), (1, "b")], (new string[] { "a", "b" }).Index());
}

[Fact]
public void Type_IsAssignableTo()
{
Assert.True(typeof(string).IsAssignableTo(typeof(object)));
Assert.True(typeof(string).IsAssignableTo(typeof(string)));
Assert.False(typeof(string).IsAssignableTo(typeof(int)));
}

[Fact]
public void CollectionBuilder()
Expand Down
2 changes: 1 addition & 1 deletion Meziantou.Polyfill/Meziantou.Polyfill.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>1.0.36</Version>
<Version>1.0.37</Version>
<TransformOnBuild>true</TransformOnBuild>

<LangVersion>preview</LangVersion>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ By default, all needed polyfills are generated. You can configure which polyfill
- `System.Text.StringBuilder.Replace(System.ReadOnlySpan<System.Char> oldValue, System.ReadOnlySpan<System.Char> newValue, System.Int32 startIndex, System.Int32 count)`
- `System.Threading.CancellationTokenSource.CancelAsync()`
- `System.Threading.Tasks.Task.WaitAsync(System.Threading.CancellationToken cancellationToken)`
- `System.Type.IsAssignableTo(System.Type? targetType)`

<!-- end_polyfills -->

Expand Down

0 comments on commit f21cca5

Please sign in to comment.