How to add classes to a list?


If i have a set of Entities (Classes) like this :

Employee, Department, Room ... etc

How to add these classes to a list instead of a list of objects :

Like this :

{Employee, Department, Room}

I want to get these classes because i have a method with the following signature :


So How to loop on a list of classes and Pass them to this method e.g :


Accepted Answer


List<Type> types = new List<Type> {typeof(Employee), typeof(Department), typeof(Room)};

Or you can get type from existed object:

List<Type> types = new List<Type> {EmployeeInstance.GetType(), DepartmentInstance.GetType(), RoomInstance.GetType()};

I will try to explain difference between Employee and typeof(Employee) but better will be read this link:

Employee it's like your data contract. It have fields like X and Y.

typeof(Employee) is your type of 'contract', it have information about fields - information that contains a description about X and Y fields.

MSDN: Type

Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why