Skip to content

Commit

Permalink
Add polyfill for Type.IsAssignableFrom(...) (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored Sep 30, 2024
1 parent 787d847 commit e56ec5f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
12 changes: 10 additions & 2 deletions PolyShim.Tests/Net50/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ namespace PolyShim.Tests.Net50;

public class TypeTests
{
private class BaseClass;
private interface IBaseInterface;

private class BaseClass : IBaseInterface;

private class DerivedClass : BaseClass;

[Fact]
public void IsAssignableTo_Test()
{
typeof(DerivedClass).IsAssignableTo(typeof(BaseClass)).Should().BeTrue();
typeof(DerivedClass).IsAssignableTo(typeof(DerivedClass)).Should().BeTrue();
typeof(DerivedClass).IsAssignableTo(typeof(BaseClass)).Should().BeTrue();
typeof(DerivedClass).IsAssignableTo(typeof(IBaseInterface)).Should().BeTrue();
typeof(BaseClass).IsAssignableTo(typeof(BaseClass)).Should().BeTrue();
typeof(BaseClass).IsAssignableTo(typeof(DerivedClass)).Should().BeFalse();
typeof(BaseClass).IsAssignableTo(typeof(IBaseInterface)).Should().BeTrue();
typeof(IBaseInterface).IsAssignableTo(typeof(IBaseInterface)).Should().BeTrue();
typeof(IBaseInterface).IsAssignableTo(typeof(DerivedClass)).Should().BeFalse();
typeof(IBaseInterface).IsAssignableTo(typeof(BaseClass)).Should().BeFalse();
}
}
26 changes: 24 additions & 2 deletions PolyShim.Tests/NetCore20/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,37 @@ namespace PolyShim.Tests.NetCore20;

public class TypeTests
{
private class BaseClass;
private interface IBaseInterface;

private class BaseClass : IBaseInterface;

private class DerivedClass : BaseClass;

[Fact]
public void IsAssignableFrom_Test()
{
typeof(DerivedClass).IsAssignableFrom(typeof(DerivedClass)).Should().BeTrue();
typeof(DerivedClass).IsAssignableFrom(typeof(BaseClass)).Should().BeFalse();
typeof(DerivedClass).IsAssignableFrom(typeof(IBaseInterface)).Should().BeFalse();
typeof(BaseClass).IsAssignableFrom(typeof(BaseClass)).Should().BeTrue();
typeof(BaseClass).IsAssignableFrom(typeof(DerivedClass)).Should().BeTrue();
typeof(BaseClass).IsAssignableFrom(typeof(IBaseInterface)).Should().BeFalse();
typeof(IBaseInterface).IsAssignableFrom(typeof(IBaseInterface)).Should().BeTrue();
typeof(IBaseInterface).IsAssignableFrom(typeof(DerivedClass)).Should().BeTrue();
typeof(IBaseInterface).IsAssignableFrom(typeof(BaseClass)).Should().BeTrue();
}

[Fact]
public void IsSubclassOf_Test()
{
typeof(DerivedClass).IsSubclassOf(typeof(BaseClass)).Should().BeTrue();
typeof(DerivedClass).IsSubclassOf(typeof(DerivedClass)).Should().BeFalse();
typeof(DerivedClass).IsSubclassOf(typeof(BaseClass)).Should().BeTrue();
typeof(DerivedClass).IsSubclassOf(typeof(IBaseInterface)).Should().BeFalse();
typeof(BaseClass).IsSubclassOf(typeof(BaseClass)).Should().BeFalse();
typeof(BaseClass).IsSubclassOf(typeof(DerivedClass)).Should().BeFalse();
typeof(BaseClass).IsSubclassOf(typeof(IBaseInterface)).Should().BeFalse();
typeof(IBaseInterface).IsSubclassOf(typeof(IBaseInterface)).Should().BeFalse();
typeof(IBaseInterface).IsSubclassOf(typeof(DerivedClass)).Should().BeFalse();
typeof(IBaseInterface).IsSubclassOf(typeof(BaseClass)).Should().BeFalse();
}
}
4 changes: 0 additions & 4 deletions PolyShim/Net50/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ internal static partial class PolyfillExtensions
{
// https://learn.microsoft.com/en-us/dotnet/api/system.type.isassignableto
public static bool IsAssignableTo(this Type type, Type? otherType) =>
#if NETSTANDARD && !NETSTANDARD2_0_OR_GREATER
otherType?.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) == true;
#else
otherType?.IsAssignableFrom(type) == true;
#endif
}
#endif
4 changes: 4 additions & 0 deletions PolyShim/NetCore20/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

internal static partial class PolyfillExtensions
{
// https://learn.microsoft.com/en-us/dotnet/api/system.type.isassignablefrom
public static bool IsAssignableFrom(this Type type, Type otherType) =>
type.GetTypeInfo().IsAssignableFrom(otherType.GetTypeInfo());

// https://learn.microsoft.com/en-us/dotnet/api/system.type.issubclassof
public static bool IsSubclassOf(this Type type, Type otherType)
{
Expand Down

0 comments on commit e56ec5f

Please sign in to comment.