객체가 제네릭 형식인지 테스트하고 싶습니다. 나는 성공하지 않고 다음을 시도했다.
public bool Test()
{
    List<int> list = new List<int>();
    return list.GetType() == typeof(List<>);
}
내가 뭘 잘못하고 있으며 어떻게이 테스트를 수행합니까?
답변
제네릭 형식의 인스턴스인지 확인하려면 다음을 수행하십시오.
return list.GetType().IsGenericType;
그것이 일반인지 확인하려면 List<T>:
return list.GetType().GetGenericTypeDefinition() == typeof(List<>);
Jon이 지적했듯이 정확한 유형 동등성을 확인합니다. 리턴 false은 반드시 list is List<T>리턴을 의미하지는 않습니다 false(즉, 오브젝트를 List<T>변수에 지정할 수 없음 ).
답변
유형이 제네릭인지 여부를 알고 싶지는 않지만 객체가 유형 인수를 모른 채 특정 제네릭 유형의 인스턴스인지 알고 싶다고 가정합니다.
불행히도 매우 간단하지 않습니다. 제네릭 형식이 클래스 인 경우 (나쁜 경우) 그렇게 나쁘지는 않지만 인터페이스가 더 어렵습니다. 클래스 코드는 다음과 같습니다.
using System;
using System.Collections.Generic;
using System.Reflection;
class Test
{
    static bool IsInstanceOfGenericType(Type genericType, object instance)
    {
        Type type = instance.GetType();
        while (type != null)
        {
            if (type.IsGenericType &&
                type.GetGenericTypeDefinition() == genericType)
            {
                return true;
            }
            type = type.BaseType;
        }
        return false;
    }
    static void Main(string[] args)
    {
        // True
        Console.WriteLine(IsInstanceOfGenericType(typeof(List<>),
                                                  new List<string>()));
        // False
        Console.WriteLine(IsInstanceOfGenericType(typeof(List<>),
                                                  new string[0]));
        // True
        Console.WriteLine(IsInstanceOfGenericType(typeof(List<>),
                                                  new SubList()));
        // True
        Console.WriteLine(IsInstanceOfGenericType(typeof(List<>),
                                                  new SubList<int>()));
    }
    class SubList : List<string>
    {
    }
    class SubList<T> : List<T>
    {
    }
}
편집 : 의견에서 언급했듯이 인터페이스에서 작동 할 수 있습니다.
foreach (var i in type.GetInterfaces())
{
    if (i.IsGenericType && i.GetGenericTypeDefinition() == genericType)
    {
        return true;
    }
}
나는 이것 주위에 어색한 경우가있을 수 있지만 몰래 의심이 있지만 지금은 실패하는 것을 찾을 수 없습니다.
답변
동적 해법을 사용하여 더 짧은 코드를 사용할 수 있습니다. 순수한 반사보다 느릴 수 있습니다.
public static class Extension
{
    public static bool IsGenericList(this object o)
    {
       return IsGeneric((dynamic)o);
    }
    public static bool IsGeneric<T>(List<T> o)
    {
       return true;
    }
    public static bool IsGeneric( object o)
    {
        return false;
    }
}
var l = new List<int>();
l.IsGenericList().Should().BeTrue();
var o = new object();
o.IsGenericList().Should().BeFalse();
답변
다음은 제네릭 형식 검사의 가장 일반적인 경우를 다루는 가장 좋아하는 두 가지 확장 방법입니다.
와 일하다:
- 다중 (일반) 인터페이스
 - 여러 (일반) 기본 클래스
 - 
true를 반환하면 특정 제네릭 형식을 ‘아웃’하는 과부하가 있습니다 (샘플에 대한 단위 테스트 참조).
public static bool IsOfGenericType(this Type typeToCheck, Type genericType) { Type concreteType; return typeToCheck.IsOfGenericType(genericType, out concreteType); } public static bool IsOfGenericType(this Type typeToCheck, Type genericType, out Type concreteGenericType) { while (true) { concreteGenericType = null; if (genericType == null) throw new ArgumentNullException(nameof(genericType)); if (!genericType.IsGenericTypeDefinition) throw new ArgumentException("The definition needs to be a GenericTypeDefinition", nameof(genericType)); if (typeToCheck == null || typeToCheck == typeof(object)) return false; if (typeToCheck == genericType) { concreteGenericType = typeToCheck; return true; } if ((typeToCheck.IsGenericType ? typeToCheck.GetGenericTypeDefinition() : typeToCheck) == genericType) { concreteGenericType = typeToCheck; return true; } if (genericType.IsInterface) foreach (var i in typeToCheck.GetInterfaces()) if (i.IsOfGenericType(genericType, out concreteGenericType)) return true; typeToCheck = typeToCheck.BaseType; } } 
다음은 (기본) 기능을 보여주는 테스트입니다.
 [Test]
    public void SimpleGenericInterfaces()
    {
        Assert.IsTrue(typeof(Table<string>).IsOfGenericType(typeof(IEnumerable<>)));
        Assert.IsTrue(typeof(Table<string>).IsOfGenericType(typeof(IQueryable<>)));
        Type concreteType;
        Assert.IsTrue(typeof(Table<string>).IsOfGenericType(typeof(IEnumerable<>), out concreteType));
        Assert.AreEqual(typeof(IEnumerable<string>), concreteType);
        Assert.IsTrue(typeof(Table<string>).IsOfGenericType(typeof(IQueryable<>), out concreteType));
        Assert.AreEqual(typeof(IQueryable<string>), concreteType);
    }
답변
return list.GetType().IsGenericType;
답변
