How can I get the type an object in a generic list assigned to an abstract class in C#?

时间:2016-04-04 18:00:31

标签: c# object generic-list

System.Generic.List<BaseBlock> BlockList = new System.Generic.List<BaseBlock>();

BaseBlock is an abstract class. Later in the program I add some objects to the list. Knowing the index of the object, how can I get the specific (not abstract) type / class of it?

1 个答案:

答案 0 :(得分:1)

Knowing the index of the object, how can I get the specific (not abstract) type / class of it?

To find the actual type of the object at index i use

Type t = BlockList[i].GetType();

GetType gives you the actual type of an object, regardless of the type of variable or container it's in.