mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-11 10:45:05 +00:00
feat: Added Type extension method for getting inner generic types
This commit is contained in:
@@ -208,13 +208,39 @@ namespace ICD.Common.Utils.Extensions
|
||||
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>
|
||||
/// Returns the given type, all base types, and all implemented interfaces.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[NotNull]
|
||||
public static IEnumerable<Type> GetAllTypes([NotNull]this Type extends)
|
||||
public static IEnumerable<Type> GetAllTypes([NotNull] this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
Reference in New Issue
Block a user