Files
ICD.Common.Utils/ICD.Common.Utils/Extensions/TypeExtensions.cs
Chris Cameron 4f46a3ebda Tidying
2017-11-16 10:10:05 -05:00

81 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
#if SIMPLSHARP
using Crestron.SimplSharp.Reflection;
#else
using System.Reflection;
#endif
namespace ICD.Common.Utils.Extensions
{
public static class TypeExtensions
{
public static Assembly GetAssembly(this Type extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
return extends
#if SIMPLSHARP
.GetCType()
#else
.GetTypeInfo()
#endif
.Assembly;
}
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);
}
}
}