首页 文章

如何从泛型类或方法的成员中获取T的类型?

提问于
浏览
580

假设我在类或方法中有一个泛型成员,所以:

public class Foo<T>
{
    public List<T> Bar { get; set; }

    public void Baz()
    {
        // get type of T
    }   
}

当我实例化该类时, T 变为 MyTypeObject1 ,因此该类具有通用列表属性: List<MyTypeObject1> . 这同样适用于非泛型类中的泛型方法:

public class Foo
{
    public void Bar<T>()
    {
        var baz = new List<T>();

        // get type of T
    }
}

我想知道,我的类列表包含什么类型的对象 . 所以名为 Bar 的列表属性或局部变量 baz 包含什么类型的 T

我不能做 Bar[0].GetType() ,因为列表可能包含零元素 . 我该怎么做?

16 回答

  • 42
    public bool IsCollection<T>(T value){
      var valueType = value.GetType();
      return valueType.IsArray() || typeof(IEnumerable<object>).IsAssignableFrom(valueType) || typeof(IEnumerable<T>).IsAssignableFrom(valuetype);
    }
    
  • 3

    如果您不需要整个Type变量并且只想检查类型,则可以轻松创建临时变量并使用is运算符 .

    T checkType = default(T);
    
    if (checkType is MyClass)
    {}
    
  • 8

    尝试

    list.GetType().GetGenericArguments()
    
  • 0

    如果您想知道属性的基础类型,请尝试以下方法:

    propInfo.PropertyType.UnderlyingSystemType.GenericTypeArguments[0]
    
  • 0

    必须在实例的基类型上设置 GetGenericArgument() 方法(其类是通用类 myClass<T> ) . 否则,它返回一个类型[0]

    例:

    Myclass<T> instance = new Myclass<T>();
    Type[] listTypes = typeof(instance).BaseType.GetGenericArguments();
    
  • 575

    您可以从任何实现IEnumerable <T>的集合类型中获取“T”类型,具体如下:

    public static Type GetCollectionItemType(Type collectionType)
    {
        var types = collectionType.GetInterfaces()
            .Where(x => x.IsGenericType 
                && x.GetGenericTypeDefinition() == typeof(IEnumerable<>))
            .ToArray();
        // Only support collections that implement IEnumerable<T> once.
        return types.Length == 1 ? types[0].GetGenericArguments()[0] : null;
    }
    

    请注意,它不支持两次实现IEnumerable <T>的集合类型,例如

    public class WierdCustomType : IEnumerable<int>, IEnumerable<string> { ... }
    

    我想如果你需要支持这个,你可以返回一系列类型......

    此外,如果您正在执行此操作(例如,在循环中),您可能还希望缓存每个集合类型的结果 .

  • 8
    public string ListType<T>(T value){
        var valueType = value.GetType().GenericTypeArguments[0].FullName;
        return value;
    }
    

    您可以使用此选项重新运行通用列表类型 .

  • 0

    如果我理解正确,您的列表与容器类本身具有相同的类型参数 . 如果是这种情况,那么:

    Type typeParameterType = typeof(T);
    

    如果您处于将 object 作为类型参数的幸运情况,请参阅Marc's answer .

  • 5

    考虑一下:我用它以相同的方式导出20个类型列表:

    private void Generate<T>()
    {
        T item = (T)Activator.CreateInstance(typeof(T));
    
        ((T)item as DemomigrItemList).Initialize();
    
        Type type = ((T)item as DemomigrItemList).AsEnumerable().FirstOrDefault().GetType();
        if (type == null) return;
        if (type != typeof(account)) //account is listitem in List<account>
        {
            ((T)item as DemomigrItemList).CreateCSV(type);
        }
    }
    
  • 29

    我使用这种扩展方法来完成类似的事情:

    public static string GetFriendlyTypeName(this Type t)
    {
        var typeName = t.Name.StripStartingWith("`");
        var genericArgs = t.GetGenericArguments();
        if (genericArgs.Length > 0)
        {
            typeName += "<";
            foreach (var genericArg in genericArgs)
            {
                typeName += genericArg.GetFriendlyTypeName() + ", ";
            }
            typeName = typeName.TrimEnd(',', ' ') + ">";
        }
        return typeName;
    }
    

    你这样使用它:

    [TestMethod]
    public void GetFriendlyTypeName_ShouldHandleReallyComplexTypes()
    {
        typeof(Dictionary<string, Dictionary<string, object>>).GetFriendlyTypeName()
            .ShouldEqual("Dictionary<String, Dictionary<String, Object>>");
    }
    

    这不是你想要的,但它有助于展示所涉及的技术 .

  • -9

    这就是我做到的

    internal static Type GetElementType(this Type type)
    {
            //use type.GenericTypeArguments if exist 
            if (type.GenericTypeArguments.Any())
             return type.GenericTypeArguments.First();
    
             return type.GetRuntimeProperty("Item").PropertyType);
    }
    

    然后这样称呼它

    var item = Activator.CreateInstance(iListType.GetElementType());
    

    要么

    var item = Activator.CreateInstance(Bar.GetType().GetElementType());
    
  • 1

    类型:

    type = list.AsEnumerable().SingleOrDefault().GetType();
    
  • 2

    使用3dGrabber的解决方案:

    public static T GetEnumeratedType<T>(this IEnumerable<T> _)
    {
        return default(T);
    }
    
    //and now 
    
    var list = new Dictionary<string, int>();
    var stronglyTypedVar = list.GetEnumeratedType();
    
  • 13

    使用以下扩展方法,您可以在没有反射的情况下逃脱:

    public static Type GetListType<T>(this List<T> _)
    {
        return typeof(T);
    }
    

    或者更一般:

    public static Type GetEnumeratedType<T>(this IEnumerable<T> _)
    {
        return typeof(T);
    }
    

    用法:

    List<string>        list    = new List<string> { "a", "b", "c" };
    IEnumerable<string> strings = list;
    IEnumerable<object> objects = list;
    
    Type listType    = list.GetListType();           // string
    Type stringsType = strings.GetEnumeratedType();  // string
    Type objectsType = objects.GetEnumeratedType();  // BEWARE: object
    
  • 477

    这对我有用 . myList是一些未知的列表 .

    IEnumerable myEnum = myList as IEnumerable;
    Type entryType = myEnum.AsQueryable().ElementType;
    
  • 9

    (注意:我假设你所知道的只是 objectIList 或类似的,并且列表在运行时可以是任何类型)

    如果你知道它是 List<T> ,那么:

    Type type = abc.GetType().GetGenericArguments()[0];
    

    另一种选择是查看索引器:

    Type type = abc.GetType().GetProperty("Item").PropertyType;
    

    使用新的TypeInfo:

    using System.Reflection;
    // ...
    var type = abc.GetType().GetTypeInfo().GenericTypeArguments[0];
    

相关问题