diff --git a/ICD.Common.Utils/Extensions/TypeExtensions.cs b/ICD.Common.Utils/Extensions/TypeExtensions.cs index 9810b26..4e7060b 100644 --- a/ICD.Common.Utils/Extensions/TypeExtensions.cs +++ b/ICD.Common.Utils/Extensions/TypeExtensions.cs @@ -208,13 +208,39 @@ namespace ICD.Common.Utils.Extensions return to.IsAssignableFrom(from); } + /// + /// Extracts the inner Types from this current Type inheriting/implementing the given generic Type. + /// E.g. + /// typeof(List<int>).GetInnerGenericTypes(typeof(IEnumerable<>)); + /// Returns + /// [ typeof(int) ] + /// + /// + /// + /// + [NotNull] + public static IEnumerable GetInnerGenericTypes([NotNull] this Type extends, Type genericType) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (genericType == null) + throw new ArgumentNullException("genericType"); + + return extends + .GetAllTypes() + .First(t => t.IsGenericType && + t.GetGenericTypeDefinition() == genericType) + .GetGenericArguments(); + } + /// /// Returns the given type, all base types, and all implemented interfaces. /// /// /// [NotNull] - public static IEnumerable GetAllTypes([NotNull]this Type extends) + public static IEnumerable GetAllTypes([NotNull] this Type extends) { if (extends == null) throw new ArgumentNullException("extends");