TypeExtension methods for getting base types from a type

This commit is contained in:
Chris Cameron
2017-07-28 12:05:49 -04:00
parent 5bd9415168
commit 0a656f0058
2 changed files with 120 additions and 7 deletions

View File

@@ -0,0 +1,64 @@
using NUnit.Framework;
using System;
using System.Linq;
namespace ICD.Common.Utils.Extensions
{
[TestFixture]
public sealed class TypeExtensionsTest
{
[Test]
public void IsAssignableToTest()
{
Assert.IsTrue(typeof(string).IsAssignableTo(typeof(object)));
Assert.IsFalse(typeof(object).IsAssignableTo(typeof(string)));
}
[Test]
public void GetAllTypesTest()
{
Type[] allTypes = typeof(B).GetAllTypes().ToArray();
Assert.AreEqual(6, allTypes.Length);
Assert.IsTrue(allTypes.Contains(typeof(E)));
Assert.IsTrue(allTypes.Contains(typeof(D)));
Assert.IsTrue(allTypes.Contains(typeof(C)));
Assert.IsTrue(allTypes.Contains(typeof(B)));
Assert.IsTrue(allTypes.Contains(typeof(A)));
Assert.IsTrue(allTypes.Contains(typeof(object)));
}
[Test]
public void GetBaseTypesTest()
{
Type[] baseTypes = typeof(B).GetBaseTypes().ToArray();
Assert.AreEqual(2, baseTypes.Length);
Assert.IsFalse(baseTypes.Contains(typeof(B)));
Assert.IsTrue(baseTypes.Contains(typeof(A)));
Assert.IsTrue(baseTypes.Contains(typeof(object)));
}
private interface C
{
}
private interface D
{
}
private interface E : C, D
{
}
private class A
{
}
private class B : A, E
{
}
}
}