代码之家  ›  专栏  ›  技术社区  ›  Luiscencio

声明类型列表

  •  23
  • Luiscencio  · 技术社区  · 15 年前

    我要声明一个基本上包含类型的列表:

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

    这有可能吗?

    5 回复  |  直到 15 年前
        1
  •  50
  •   Yannick Motton    15 年前

    试试这个:

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

    这个 typeof() 运算符用于返回 System.Type 一种类型。

    对于对象实例,可以调用 GetType() 方法继承自对象。

        2
  •  13
  •   Adam Sills    15 年前

    你的代码几乎都有了。使用type of,而不仅仅是类型的名称。

    List<Type> types = new List<Type>() {typeof(Button), typeof(TextBox) };
    
        3
  •  5
  •   Matt Brunell    15 年前

    是的,使用 List<System.Type>

    var types = new List<System.Type>();
    

    要向列表中添加项目,请使用typeof关键字。

    types.Add(typeof(Button));
    types.Add(typeof(CheckBox));
    
        4
  •  4
  •   Greg    15 年前
    List<Type> types = new List<Type>{typeof(String), typeof(Int32) };
    

    您需要使用typeof关键字。

        5
  •  1
  •   3Dave    15 年前

    使用类型化泛型列表:

    List<Type> lt = new List<Type>();