Как я могу видеть, реализует ли GenericTypeDefinition IEnumerable <>

У меня есть метод, который проверяет, универсален ли тип и затем проверяет, имеет ли GenericTypeDefinition IEnumerable<>.

static Type GetEnumerableType(Type type)  
{  
    if(type.IsGenericType) {  
        var genericTypeDefinition = type.GetGenericTypeDefinition();  
        if (genericTypeDefinition == typeof(IEnumerable<>)) {  
            return type.GetGenericArguments()[0];  
        }  
    }  
    return null;  
}

Работы как очарование, если это - IEnumerable. Если GenericTypeDefinition IList<> или List<> это не работает. Я попробовал..

typeof(IEnumerable<>).IsAssignableFrom(genericTypeDefinition)

.. без успеха. Конечно, должен быть лучший путь, затем объединяющий еще-операторы в цепочку?

6
задан Jon Cage 23 February 2010 в 16:16
поделиться

2 ответа

Вы можете использовать GetInterfaces , чтобы проверить, реализует ли тип IEnumerable <> так

Type type = new List<string>().GetType();

if (type.IsGenericType) 
{
    var genericTypeDefinition = type.GetGenericTypeDefinition();

    if (genericTypeDefinition.GetInterfaces()
                .Any( t => t.IsGenericType && 
                           t.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
    {
        return type.GetGenericArguments()[0];
    }
}
12
ответ дан 8 December 2019 в 12:19
поделиться

Я закончил более глубокое погружение в общую обработку объектов и нашел этот способ более сложным, чем любые исходные предположения. Я сейчас использую следующий метод:

/// <summary>Check whether the specified type is enumerable.</summary>
/// <param name="type">The type.</param>
/// <param name="underlyingType">IEnumerable{int} would be int</param>
/// <param name="excludeString">
///  [OPTIONAL] if set to <c>true</c> [exclude string]. Strings are enumerable as char[]
///  this is likely not something you want. Default is true (string will return false)
/// </param>
/// <returns><c>true</c> supplied type is enumerable otherwise <c>false</c></returns>
public static bool IsEnumerable(this Type type, out Type underlyingType,
                                bool excludeString = true)
{
    underlyingType = null;

    if (type.IsEnum || type.IsPrimitive || type.IsValueType) return false;

    if (excludeString && type == typeof(string)) return false;

    if (type.IsGenericType)
    {
        if (type.IsTypeDefinitionEnumerable() ||
            type.GetInterfaces()
                .Any(t => t.IsSelfEnumerable() || t.IsTypeDefinitionEnumerable()))
        {
            underlyingType = type.GetGenericArguments()[0];
            return true;
        }
    }
    //direct implementations of IEnumerable<T>, inheritance from List<T> etc
    var enumerableOrNull = type.GetInterfaces()
                               .FirstOrDefault(t => t.IsTypeDefinitionEnumerable());
    if (enumerableOrNull == null) return false;

    underlyingType = enumerableOrNull.GetGenericArguments()[0];
    return true;
}

//

private static bool IsSelfEnumerable(this Type type)
{
    bool isDirectly = type == typeof(IEnumerable<>);
    return isDirectly;
}

private static bool IsTypeDefinitionEnumerable(this Type type)
{
    bool isViaInterfaces = type.IsGenericType && 
                           type.GetGenericTypeDefinition().IsSelfEnumerable();
    return isViaInterfaces;
}

Это решение протестировано:

Install-Package NUnit -Version -Version 2.6.4

Install-Package Shouldly

[Test]
public void List_is_enumerable()
{
    var sut = new List<int>();

    Type underlyingType;
    var result = sut.IsEnumerable(out underlyingType);

    result.ShouldBeTrue();
    underlyingType.ShouldBe(typeof(int));
}

//

[Test]
public void Yield_return_is_enumerable()
{
    var sut = Yielded();

    Type underlyingType;
    var result = sut.IsEnumerable(out underlyingType);

    result.ShouldBeTrue();
    underlyingType.ShouldBe(typeof(int));
}

private IEnumerable<int> Yielded()
{
    for (int i = 0; i < 3; i++)
    {
        yield return i;
    }
}

//

[Test]
public void int_is_not_an_enumerable()
{
    var sut = 5;

    Type underlyingType;
    var result = sut.IsEnumerable(out underlyingType);

    result.ShouldBe(false);
    underlyingType.ShouldBeNull();
}

[Test]
public void object_is_not_an_enumerable()
{
    var sut = new { foo = 1};

    Type underlyingType;
    var result = sut.IsEnumerable(out underlyingType);

    result.ShouldBe(false);
    underlyingType.ShouldBeNull();
}

Сохранено для потомков. Это не отвечает на исходный вопрос, но было явно полезно для участников.

public static bool IsA<T>(this Type type)
{
    return typeof (T).IsAssignableFrom(type);
}
7
ответ дан 8 December 2019 в 12:19
поделиться
Другие вопросы по тегам:

Похожие вопросы: