Button b = new Button() ;
Type t = b.GetType() ;

解决方案 »

  1.   

    谢谢,不过我就是不明白为什么这样不行呢?如果象下面这样,创建出来的不知道是什么东西Assembly asm = Assembly.LoadWithPartialName("System.Windows.Forms");Type t = asm.GetType("System.Windows.Forms.Button"); //t值不为nullobject o = Activator.CreateInstance(t);   //创建出来的不是Button的实例,奇怪
      

  2.   

    Assembly asm = Assembly.LoadWithPartialName("System.Windows.Forms"); Type t = asm.GetType("System.Windows.Forms.Button"); //t值不为null object o = Activator.CreateInstance(t);   //创建出来的不是Button的实例,奇怪 Button b = (Button)o;
    b.Parent= this;
      

  3.   

    type的GetType方法有使用时有诸多限制,最好的办法从实例来生成。tuzi98(兔子) 的方案可行
      

  4.   

    我已判断了
    if (o is Button) //不是button
    {
    Control c = (Control)o;
    this.Controls.Add(c);

    我想可能是GetType的限制吧查了一下帮助,可是不懂什么意思GetType 仅适用于从磁盘加载的程序集。如果调用 GetType 来查找在用 System.Reflection.Emit 服务定义的动态程序集中定义的类型,则可能会获得不一致的行为。行为取决于动态程序集是否持久,即是否使用 System.Reflection.Emit.AssemblyBuilderAccess 枚举的 RunAndSave 或 Save 访问模式来创建。如果动态程序集持久并已在调用 GetType 之前写入磁盘,则加载器会在磁盘上查找已保存的程序集,加载该程序集并从该程序集中检索类型。如果在调用 GetType 时程序集尚未保存到磁盘中,则此方法返回空引用(Visual Basic 中为 Nothing)。GetType 不识别瞬态动态程序集,因此通过调用 GetType 来检索瞬态动态程序集中的类型将返回空引用 (Nothing)。
      

  5.   

    用以下方法可行/
    Assembly asm = Assembly.LoadWithPartialName("System.Windows.Forms");Type t = typeof(System.Windows.Forms.Button); //t值不为nullobject o = Activator.CreateInstance(t);
      

  6.   

    用Type.GetType()得到的是已加载的程序集中的类型信息,你的System.Windows.Forms.Button没有加载进去,所以Type.GetType()无法返回类型信息,所以仅得到一个null了
      

  7.   

    不需要预先引用System.Windows.Forms.dll,更不需要先建立Button对象。
    只需要在GetType参数里指明定义Button的Assembly。Type t = Type.GetType("System.Windows.Forms.Button, System.Windows.Forms, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
      

  8.   

    谢谢qqchen79(知秋一叶 [MS MVP]) 不过这些信息怎么获得呢?Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089");
      

  9.   

    you can see it when using ildasm.
    or in your program, Assembly.FullName.