Skip to content

Instantly share code, notes, and snippets.

@q42jaap
Created June 15, 2012 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save q42jaap/2936304 to your computer and use it in GitHub Desktop.
Save q42jaap/2936304 to your computer and use it in GitHub Desktop.
GetGenericArgumentsForBaseType
static Type[] GetGenericArgumentsForBaseType(Type givenType, Type genericType)
{
if (genericType.IsInterface)
{
var interfaceTypes = givenType.GetInterfaces();
foreach (var it in interfaceTypes)
{
if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)
return it.GetGenericArguments();
}
}
else
{
Type baseType = givenType;
while (baseType != null)
{
if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == genericType)
return baseType.GetGenericArguments();
baseType = baseType.BaseType;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment