Skip to content

Instantly share code, notes, and snippets.

@djeikyb
Created April 28, 2022 19:57
Show Gist options
  • Save djeikyb/144720d4a3d97ae7ff05df9c14739fb5 to your computer and use it in GitHub Desktop.
Save djeikyb/144720d4a3d97ae7ff05df9c14739fb5 to your computer and use it in GitHub Desktop.
using System;
using Xunit;
using Xunit.Abstractions;
namespace Example;
public class TypeChecking
{
private readonly ITestOutputHelper _testOutputHelper;
public TypeChecking(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
public class SomeParent { }
public class SomeChild : SomeParent { }
[Theory]
[InlineData(typeof(SomeParent))]
[InlineData(typeof(SomeChild))]
[InlineData(typeof(string))]
public void TypeCheckingB(Type a)
{
Type b = typeof(SomeParent);
_testOutputHelper.WriteLine($"Asking if same:\n a) {a}\n b) {b}");
_testOutputHelper.WriteLine(string.Empty);
if (a == b)
{
_testOutputHelper.WriteLine("a == b");
}
else
{
_testOutputHelper.WriteLine("a != b");
}
if (a.IsInstanceOfType(b))
{
_testOutputHelper.WriteLine("a IsInstanceOfType b");
}
else
{
_testOutputHelper.WriteLine("NOT a IsInstanceOfType b");
}
if (a.IsAssignableTo(b))
{
_testOutputHelper.WriteLine("a IsAssignableTo b");
}
else
{
_testOutputHelper.WriteLine("NOT a IsAssignableTo b");
}
if (a.IsAssignableFrom(b))
{
_testOutputHelper.WriteLine("a IsAssignableFrom b");
}
else
{
_testOutputHelper.WriteLine("NOT a IsAssignableFrom b");
}
if (b.IsAssignableTo(a))
{
_testOutputHelper.WriteLine("b IsAssignableTo a");
}
else
{
_testOutputHelper.WriteLine("NOT b IsAssignableTo a");
}
if (b.IsAssignableFrom(a))
{
_testOutputHelper.WriteLine("b IsAssignableFrom a");
}
else
{
_testOutputHelper.WriteLine("NOT b IsAssignableFrom a");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment