请问在反射中
Type t = Type.GetType("名词空间.类名")

Type t =typeof(名词空间.类名)
有什么区别?我用前者有时候能反射出来,有时候出不来.
后者可以出来.我想知道他们2个的区别谢谢

解决方案 »

  1.   

    好象Type.GetType要在本程序集中
    typeof都可以
      

  2.   

    用typeof必须引用包含这个类型的程序集,而用Type.GetType()则不需要,只要在执行路径下可以找到类型所在的程序集即可
      

  3.   

    Type t = Type.GetType("名词空间.类名")
    是用字符串的形式,这样的好处在于可以拼接。或者你反射其它程序的时候,就只能够靠这样的方式了。Type t =typeof(名词空间.类名)
    是从一个类直接得到Type,只能够应用于当前程序集内。
    typeof效率比GetType高
      

  4.   

    如果程序集没有加载,用Type.GetType就得不到type对象,它是运行时得到的
    就像viena说的一样
      

  5.   

    viena(维也纳N02) 前者是运行时通过方法得到的
    后者则是编译时得到的
    明显后者要优于前者,但只能用于你的类或你引用的类
    而前者没有这个限制既然后者是编译时得到,那效率应该比前者低啊?
    typeof比Type.GetType()低才对啊?
    但是为什么我用Type.GetType()的时候我有时候取不到值是为什么?
    比如我有个类是TestClass
    我把他的DLL加载进去了,然后Type t = Type.GetType("TestClass的名词空间.TestClass")
    我单步调试t依然是null
    这是为什么呢?
      

  6.   

    这是我TestClass这个类
    public class TestClass
    {
            private string info;
            public TestClass(string Info)
            {
                info = Info ;
            }        public string GetValue()
            {
                return info;
            }
    }然后我的程序
            Type t = Type.GetType("TestClass");
            Object[] Paras = new object[]{"wwwwwojgkfdgl"};
            TestClass test = (TestClass)Activator.CreateInstance(t,Paras);
            this.Label1.Text = test.GetValue();这里这个t始终是null
    但我用typeof就可以正常
    我就想知道这是为什么?(PS:我是2005做的没名词空间,直接工程下建立的TestClass)
      

  7.   

    If no assembly is specified, Type.GetType() will only look in the calling assembly and then mscorlib.dll for the type. For it to look in any other assembly, you need to give the Type.AssemblyQualifiedName for the type.
      

  8.   

    呵呵 确实应该先编译效率高 我糊涂了~~~我是要放在缓存里的
    因为用方式灵活些但我GetType出不来的问题还是没解决:(
      

  9.   

    to:scow(怡红快绿之小橙子) 
    AssemblyQualifiedName:这个合法的装备件名称是什么意思?
    怎么才能是合法的装备件名称
    我就觉得我这个Type.GetType()这里的名称不对
    是不一少了什么比如注册之类的?