mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
feat: Added Type extension method for getting inner generic types
This commit is contained in:
@@ -208,6 +208,32 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
return to.IsAssignableFrom(from);
|
return to.IsAssignableFrom(from);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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) ]
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="genericType"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[NotNull]
|
||||||
|
public static IEnumerable<Type> 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();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the given type, all base types, and all implemented interfaces.
|
/// Returns the given type, all base types, and all implemented interfaces.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user