ln.type/TypeExtensions.cs

22 lines
848 B
C#

using System;
using System.Linq;
namespace ln.type
{
public static class TypeExtensions
{
public static bool HasGenericInterface<T>(this Type type) => HasGenericInterface(type, typeof(T).GetGenericTypeDefinition());
public static bool HasGenericInterface(this Type type, Type genericInterfaceType)
{
return type.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == genericInterfaceType);
}
public static Type GetGenericInterface<T>(this Type type) => GetGenericInterface(type, typeof(T).GetGenericTypeDefinition());
public static Type GetGenericInterface(this Type type, Type genericInterfaceType)
{
return type.GetInterfaces().First(x => x.IsGenericType && x.GetGenericTypeDefinition() == genericInterfaceType);
}
}
}