【发布时间】:2010-12-09 14:23:15
【问题描述】:
我想声明一个包含类型的列表:
List<Type> types = new List<Type>() {Button, TextBox };
这可能吗?
【问题讨论】:
-
我正在开发一组函数来验证/验证用户在表单中输入的数据。
我想声明一个包含类型的列表:
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
您的代码几乎可以使用它。使用 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) };
你需要使用 typeof 关键字。
【讨论】:
使用类型化的通用列表:
List<Type> lt = new List<Type>();
【讨论】: