public static FindConstructedTypes ( IType type, IType definition ) : IEnumerable |
||
type | IType | The type in whose hierarchy to search for constructed types. |
definition | IType | The generic type definition whose constructed versions to search for. |
return | IEnumerable |
public static IEnumerable<IType> FindConstructedTypes(IType type, IType definition)
{
while (type != null)
{
// Check if type itself is constructed from the definition
if (type.ConstructedInfo != null &&
type.ConstructedInfo.GenericDefinition == definition)
{
yield return type;
}
// Look in type's immediate interfaces
IType[] interfaces = type.GetInterfaces();
if (interfaces != null)
{
foreach (IType interfaceType in interfaces)
{
foreach (IType match in FindConstructedTypes(interfaceType, definition))
{
yield return match;
}
}
}
// Move on to the type's base type
type = type.BaseType;
}
}
public IType GetGenericEnumerableItemType(IType iteratorType) { // Arrays implicitly implement IEnumerable[of element type] if (iteratorType is ArrayType) { return(iteratorType.ElementType); } // If type is not an array, try to find IEnumerable[of some type] in its interfaces IType itemType = null; foreach (IType type in GenericsServices.FindConstructedTypes(iteratorType, IEnumerableGenericType)) { IType candidateItemType = type.ConstructedInfo.GenericArguments[0]; if (itemType != null) { itemType = GetMostGenericType(itemType, candidateItemType); } else { itemType = candidateItemType; } } return(itemType); }