Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a List of types

Tags:

c#

types

list

I want to declare a list containing types basically:

List<Type> types = new List<Type>() {Button, TextBox };

is this possible?

like image 666
Luiscencio Avatar asked Oct 23 '09 20:10

Luiscencio


2 Answers

Try this:

List<Type> types = new List<Type>() { typeof(Button), typeof(TextBox) };

The typeof() operator is used to return the System.Type of a type.

For object instances you can call the GetType() method inherited from Object.

like image 87
Yannick Motton Avatar answered Nov 08 '22 16:11

Yannick Motton


You almost have it with your code. Use typeof and not just the name of the type.

List<Type> types = new List<Type>() {typeof(Button), typeof(TextBox) };
like image 14
Adam Sills Avatar answered Nov 08 '22 14:11

Adam Sills