mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-10 02:05:20 +00:00
Extension method for getting the immediate interfaces on a Type
This commit is contained in:
@@ -47,6 +47,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
|
||||
private static readonly Dictionary<Type, Type[]> s_TypeAllTypes;
|
||||
private static readonly Dictionary<Type, Type[]> s_TypeBaseTypes;
|
||||
private static readonly Dictionary<Type, Type[]> s_TypeImmediateInterfaces;
|
||||
private static readonly Dictionary<Type, Type[]> s_TypeMinimalInterfaces;
|
||||
|
||||
/// <summary>
|
||||
@@ -56,6 +57,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
{
|
||||
s_TypeAllTypes = new Dictionary<Type, Type[]>();
|
||||
s_TypeBaseTypes = new Dictionary<Type, Type[]>();
|
||||
s_TypeImmediateInterfaces = new Dictionary<Type, Type[]>();
|
||||
s_TypeMinimalInterfaces = new Dictionary<Type, Type[]>();
|
||||
}
|
||||
|
||||
@@ -184,6 +186,35 @@ namespace ICD.Common.Utils.Extensions
|
||||
} while (type != null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the interfaces that the given type implements that are not implemented further
|
||||
/// down the inheritance chain.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Type> GetImmediateInterfaces(this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (!s_TypeImmediateInterfaces.ContainsKey(extends))
|
||||
{
|
||||
IEnumerable<Type> allInterfaces = extends.GetInterfaces();
|
||||
|
||||
IEnumerable<Type> childInterfaces =
|
||||
extends.GetAllTypes()
|
||||
.Except(extends)
|
||||
.SelectMany(t => t.GetImmediateInterfaces())
|
||||
.Distinct();
|
||||
|
||||
Type[] immediateInterfaces = allInterfaces.Except(childInterfaces).ToArray();
|
||||
|
||||
s_TypeImmediateInterfaces.Add(extends, immediateInterfaces);
|
||||
}
|
||||
|
||||
return s_TypeImmediateInterfaces[extends];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the smallest set of interfaces for the given type that cover all implemented interfaces.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user