亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C#泛型列表<T>如何獲取T的類型?

C#泛型列表<T>如何獲取T的類型?

C#
呼啦一陣風 2019-10-09 15:18:07
我正在做一個反思項目,現在被困了。如果我有一個可以容納列表的“ myclass”對象,如果myclass.SomList屬性為空,是否有人知道如何獲取以下代碼中的類型?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;            }        }
查看完整描述

3 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

Type type = pi.PropertyType;

if(type.IsGenericType && type.GetGenericTypeDefinition()

        == typeof(List<>))

{

    Type itemType = type.GetGenericArguments()[0]; // use this...

}

通常,要支持any IList<T>,您需要檢查接口:


foreach (Type interfaceType in type.GetInterfaces())

{

    if (interfaceType.IsGenericType &&

        interfaceType.GetGenericTypeDefinition()

        == typeof(IList<>))

    {

        Type itemType = type.GetGenericArguments()[0];

        // do something...

        break;

    }

}


查看完整回答
反對 回復 2019-10-09
?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

給定一個對象,我懷疑是某種的IList<>,我怎么能確定的東西它是一個IList<>?


這是勇敢的解決方案。它假定您具有要測試的實際對象(而不是Type)。


public static Type ListOfWhat(Object list)

{

    return ListOfWhat2((dynamic)list);

}


private static Type ListOfWhat2<T>(IList<T> list)

{

    return typeof(T);

}

用法示例:


object value = new ObservableCollection<DateTime>();

ListOfWhat(value).Dump();

版畫


typeof(DateTime)


查看完整回答
反對 回復 2019-10-09
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

Marc的答案是我為此使用的方法,但是為了簡單起見(以及更友好的API?),您可以在集合基類中定義一個屬性,如果您具有以下屬性:


public abstract class CollectionBase<T> : IList<T>

{

   ...


   public Type ElementType

   {

      get

      {

         return typeof(T);

      }

   }

}

我發現這種方法很有用,并且對于任何泛型新手來說都很容易理解。


查看完整回答
反對 回復 2019-10-09
  • 3 回答
  • 0 關注
  • 3691 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號