mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 12:45:01 +00:00
Extension method for finding the minimal interfaces that a given type implements
This commit is contained in:
@@ -46,7 +46,8 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
};
|
};
|
||||||
|
|
||||||
private static readonly Dictionary<Type, Type[]> s_TypeAllTypes;
|
private static readonly Dictionary<Type, Type[]> s_TypeAllTypes;
|
||||||
private static readonly Dictionary<Type, Type[]> s_TypeBaseTypes;
|
private static readonly Dictionary<Type, Type[]> s_TypeBaseTypes;
|
||||||
|
private static readonly Dictionary<Type, Type[]> s_TypeMinimalInterfaces;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Static constructor.
|
/// Static constructor.
|
||||||
@@ -55,6 +56,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
{
|
{
|
||||||
s_TypeAllTypes = new Dictionary<Type, Type[]>();
|
s_TypeAllTypes = new Dictionary<Type, Type[]>();
|
||||||
s_TypeBaseTypes = new Dictionary<Type, Type[]>();
|
s_TypeBaseTypes = new Dictionary<Type, Type[]>();
|
||||||
|
s_TypeMinimalInterfaces = new Dictionary<Type, Type[]>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -181,5 +183,28 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
yield return type;
|
yield return type;
|
||||||
} while (type != null);
|
} while (type != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the smallest set of interfaces for the given type that cover all implemented interfaces.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IEnumerable<Type> GetMinimalInterfaces(this Type extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
if (!s_TypeMinimalInterfaces.ContainsKey(extends))
|
||||||
|
{
|
||||||
|
Type[] allInterfaces = extends.GetInterfaces();
|
||||||
|
Type[] minimalInterfaces =
|
||||||
|
allInterfaces.Except(allInterfaces.SelectMany(t => t.GetInterfaces()))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
s_TypeMinimalInterfaces.Add(extends, minimalInterfaces);
|
||||||
|
}
|
||||||
|
|
||||||
|
return s_TypeMinimalInterfaces[extends];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user