C# - Get the item type for a generic list

前端 未结 9 2032
小鲜肉
小鲜肉 2020-12-05 06:15

What would be the best way of getting the type of items a generic list contains? It\'s easy enough to grab the first item in the collection and call .GetType(), but I can\'

9条回答
  •  生来不讨喜
    2020-12-05 07:05

    You could use the Type.GetGenericArguments method for this purpose.

    List myList = ...
    
    Type myListElementType = myList.GetType().GetGenericArguments().Single();
    

提交回复
热议问题