我想声明一个基本包含类型的列表:
List<Type> types = new List<Type>() {Button, TextBox };
这是否可能?
试试这个:
List<Type> types = new List<Type>() { typeof(Button), typeof(TextBox) };
typeof()
操作符用于返回类型的System.Type
。
对于对象实例,您可以调用从Object继承的GetType()
方法。
var
来修改声明,你就会得到我的赞。:P - Noldorin您的代码已经接近正确了。使用 typeof 而不仅仅是类型名称。
List<Type> types = new List<Type>() {typeof(Button), typeof(TextBox) };
是的,使用List<System.Type>
var types = new List<System.Type>();
要将项目添加到列表中,请使用 typeof 关键字。
types.Add(typeof(Button));
types.Add(typeof(CheckBox));
List<Type> types = new List<Type>{typeof(String), typeof(Int32) };
List<Type> lt = new List<Type>();