2010-08-09 92 views
1

有一些我无法理解的东西。我无法读取类型参考:找不到mscorlib的类型

Assembly mscorlib = Assembly.Load("mscorlib"); 

// it DOES exist, returns type reference: 
mscorlib.GetType("System.Deployment.Internal.Isolation.IDefinitionAppId"); 

// but its parent scope doesn't exist.. returns null: 
mscorlib.GetType("System.Deployment.Internal.Isolation"); 

// even though it exists, it doesn't compile 
// System.Deployment.Internal.Isolation.IDefinitionAppId x; 

这怎么可能?

+0

你在编译时得到的错误是什么? – fletcher 2010-08-09 18:56:47

回答

3

你最后一行不能编译的原因是因为IDefinitionAppId内部 - 不是因为System.Deployment.Internal.Isolation是一种类型。

请注意,如果Isolation是某个类型的名称,则必须使用GetType("System.Deployment.Internal.Isolation+IDefinitionAppId")(请注意+),因为这是嵌套类型在CLR名称中的表示形式。

这是非常简单的证明这一点:

using System; 
using System.Reflection; 

public class Test 
{ 
    static void Main() 
    { 
     Assembly mscorlib = typeof(string).Assembly; 
     string name = "System.Deployment.Internal.Isolation.IDefinitionAppId"; 
     Type type = mscorlib.GetType(name); 

     // Prints System.Deployment.Internal.Isolation 
     Console.WriteLine(type.Namespace); 
    } 
} 

所以System.Deployment.Internal.Isolation是一个命名空间,而不是一个类型的,因此为什么Assembly.GetType(...)没有找到它作为一种类型。

+0

内部......我知道我错过了一些东西。谢谢。 – 2010-08-09 19:04:16

1

System.Deployment.Internal.Isolation是一个命名空间,而不是一个类型,你不能获得对命名空间的“引用”,它只是完整类名的一部分。

+0

nope,智能感知不会看到这个命名空间,因此我无法达到嵌套类型IDefinitionAppId – 2010-08-09 18:57:24

+0

如果它是一个命名空间,我将能够有一个System.Deployment.Internal.Isolation.IDefinitionAppId类型的变量,你可以看到最后一行 – 2010-08-09 18:58:23

+0

@Marc:IDefinitionAppId不是嵌套类型。 – 2010-08-09 18:58:57

相关问题