为什么System.Type.GetType("System.Windows.Forms.TextBox") 返回null
而System.Type.GetType("System.Int32")可以?我想根据字符串创建一个对象
System.Type t=System.Type.GetType(str)
object o=System.Activator.CreateInstance(t)
但是str="System.Windows.Forms.TextBox"时t为什么为null?我应该怎样做?

解决方案 »

  1.   

    typeof()的括号中只能是类型,如:typeof(System.Int32),而我实现不知道类型,只知道表示类型的字符创!typeof("System.Int32")无法编译!
      

  2.   

    你确认你引用了System.Windows.Forms程序集?
      

  3.   

    我做了一下测试,出现的异常是:System.TypeLoadException was unhandled
      Message="Could not load type 'System.Windows.Forms.TextBox' from assembly 'WindowsApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."
      Source="mscorlib"
      TypeName="System.Windows.Forms.TextBox"翻了一下google,没有发现很直接的方法。
      

  4.   

    你这样做肯定是错的Type.GetType(string)必须是一个Type的全表示名,你这样的写法仅仅说明了Type的名称而已,但是CLR并不知道去哪里找这个Type,必须加上Assembly名我给你写一个例子
      

  5.   

    Type t = System.Type.GetType("System.Windows.Forms.TextBox,System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", true);你用这个试试?
      

  6.   

    我用的是.NET 2.0
    如果你用的是1.1的话,就把这个AssemblyName修改一下也就是说语法应该是
    Type.GetType("你的类型名,你的类型所在的程序集名")
    这个程序集的名称一定要是AssemblyName,而不是“System.Forms.Windows”之类的,原因就是这样还是找不到程序集;
      

  7.   

    是不是System.Type.GetType()只能获得mscorlib中的Type?我用
    System.Type t1=Assembly.GetAssembly(typeof(TextBox)).GetType("System.Windows.Forms.TextBox");System.Type t2=Assembly.LoadFile("C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\System.Windows.Forms.dll").GetType("System.Windows.Forms.TextBox");
    可以得到Type,但是觉得都不好!
      

  8.   

    Reeezak(坚持信念)  正解!
      

  9.   

    不是说了吗,一定要AssemblyName,否则会找不到程序集的System.Type.GetType()默认就找自己的类型,继承的也是System.Type.GetType(“类型”)默认找自己所在的assembly和mscorlib