C#泛型列表< T>如何让T的类型? [英] C# generic list <T> how to get the type of T?

查看:133
本文介绍了C#泛型列表< T>如何让T的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一种体现项目,现在我卡住了。
如果我有MyClass的,可容纳一个List的对象
没有人知道如何获得的类型如下如果属性myclass.SomList是空的code?

 列表< MyClass的> myList中= dataGenerator.getMyClasses();
lbxObjects.ItemsSource = myList中;
lbxObjects.SelectionChanged + = lbxObjects_SelectionChanged;私人无效lbxObjects_SelectionChanged(对象发件人,SelectionChangedEventArgs E)
        {
            反映();
        }
私人无效反映()
{
的foreach(PI的PropertyInfo在lbxObjects.SelectedItem.GetType()的GetProperties())
{
      开关(pi.PropertyType.Name.ToLower())
      {
       案list`1:
           {
            //这个工作,如果列表< T>包含一个或多个元素。
            键入tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem,NULL));            //但怎么可能拿到类型,如果该值为null?
            //我需要能够创建泛型列表期望类型的新对象。
            //类型类型= pi.getType? //如何获得内部列表和LT类的类型; T&GT ;?
             打破;
           }
      }
}
}
私有类型GetGenericType(obj对象)
        {
            如果(OBJ!= NULL)
            {
                类型t = obj.GetType();
                如果(t.IsGenericType)
                {
                    键入[在= t.GetGenericArguments]();
                    T = at.First<类型和GT;();
                }返回吨;
            }
            其他
            {
                返回null;
            }
        }


解决方案

 类型类型= pi.PropertyType.PropertyType;
如果(type.IsGenericType&安培;&安培; type.GetGenericTypeDefinition()
        == typeof运算(列表<>))
{
    键入ITEMTYPE = type.GetGenericArguments()[0]; // 用这个...
}


更一般地,支持任何的IList< T> ,你需要检查的接口:

 的foreach(类型interfaceType在type.GetInterfaces())
{
    如果(interfaceType.IsGenericType&安培;&安培;
        interfaceType.GetGenericTypeDefinition()
        == typeof运算(IList的<>))
    {
        键入ITEMTYPE = type.GetGenericArguments()[0];
        // 做一点事...
        打破;
    }
}

I’m working on a reflection project, and now I’m stuck. If I have an object of "myclass" that can hold a List does anyone know how to get the type as in the code below if the property myclass.SomList is empty?

List<myclass>  myList  =  dataGenerator.getMyClasses();
lbxObjects.ItemsSource = myList; 
lbxObjects.SelectionChanged += lbxObjects_SelectionChanged;

private void lbxObjects_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Reflect();
        }
Private void Reflect()
{
foreach (PropertyInfo pi in lbxObjects.SelectedItem.GetType().GetProperties())
{
      switch (pi.PropertyType.Name.ToLower())
      {
       case "list`1":
           {           
            // This works if the List<T> contains one or more elements.
            Type tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem, null));

            // but how is it possible to get the Type if the value is null? 
            // I need to be able to create a new object of the type the generic list expect. 
            // Type type = pi.getType?? // how to get the Type of the class inside List<T>?
             break;
           }
      }
}
}
private Type GetGenericType(object obj)
        {
            if (obj != null)
            {
                Type t = obj.GetType();
                if (t.IsGenericType)
                {
                    Type[] at = t.GetGenericArguments();
                    t = at.First<Type>();
                } return t;
            }
            else
            {
                return null;
            }
        }

解决方案

Type type = pi.PropertyType.PropertyType;
if(type.IsGenericType && type.GetGenericTypeDefinition()
        == typeof(List<>))
{
    Type itemType = type.GetGenericArguments()[0]; // use this...
}


More generally, to support any IList<T>, you need to check the interfaces:

foreach (Type interfaceType in type.GetInterfaces())
{
    if (interfaceType.IsGenericType &&
        interfaceType.GetGenericTypeDefinition()
        == typeof(IList<>))
    {
        Type itemType = type.GetGenericArguments()[0];
        // do something...
        break;
    }
}

这篇关于C#泛型列表&LT; T&GT;如何让T的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆