mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-12 19:25:00 +00:00
TypeExtension methods for getting base types from a type
This commit is contained in:
64
ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs
Normal file
64
ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs
Normal 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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,63 @@
|
||||
#if STANDARD
|
||||
using System.Reflection;
|
||||
#endif
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ICD.Common.Utils.Extensions
|
||||
{
|
||||
public static class TypeExtensions
|
||||
{
|
||||
public static bool IsAssignableTo(this Type from, Type to)
|
||||
{
|
||||
return to.IsAssignableFrom(from);
|
||||
}
|
||||
}
|
||||
public static class TypeExtensions
|
||||
{
|
||||
public static bool IsAssignableTo(this Type from, Type to)
|
||||
{
|
||||
if (from == null)
|
||||
throw new ArgumentNullException("from");
|
||||
|
||||
if (to == null)
|
||||
throw new ArgumentNullException("to");
|
||||
|
||||
return to.IsAssignableFrom(from);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the given type, all base types, and all implemented interfaces.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Type> GetAllTypes(this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
yield return extends;
|
||||
|
||||
foreach (Type type in extends.GetBaseTypes())
|
||||
yield return type;
|
||||
|
||||
foreach (Type type in extends.GetInterfaces())
|
||||
yield return type;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all base types for the given type.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Type> GetBaseTypes(this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
do
|
||||
{
|
||||
extends = extends
|
||||
#if !SIMPLSHARP
|
||||
.GetTypeInfo()
|
||||
#endif
|
||||
.BaseType;
|
||||
|
||||
if (extends != null)
|
||||
yield return extends;
|
||||
} while (extends != null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user