未找到 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;
这怎么可能?
There is something I cannot understand. I can't read the type reference:
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;
How is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后一行无法编译的原因是
IDefinitionAppId
是 internal - 而不是因为System.Deployment.Internal.Isolation
是一种类型。请注意,如果
Isolation
是类型名称,则必须使用GetType("System.Deployment.Internal.Isolation+IDefinitionAppId")
(注意 +)因为这就是 CLR 名称中嵌套类型的表示方式。演示这一点非常简单:
所以
System.Deployment.Internal.Isolation
是一个命名空间,而不是类型,因此为什么Assembly.GetType(...)
不这样做找到它作为一种类型。The reason your last line won't compile is because
IDefinitionAppId
is internal - not becauseSystem.Deployment.Internal.Isolation
is a type.Note that if
Isolation
were the name of a type, you'd have to useGetType("System.Deployment.Internal.Isolation+IDefinitionAppId")
(note the +) as that's how nested types are represented in CLR names.It's very simple to demonstrate this:
So
System.Deployment.Internal.Isolation
is a namespace, not a type, hence whyAssembly.GetType(...)
doesn't find it as a type.System.Deployment.Internal.Isolation 是一个命名空间,而不是一个类型,您无法获得对命名空间的“引用”,它只是完整类名的一部分。
System.Deployment.Internal.Isolation is a namespace, not a type, you can't get a "reference" to a namespace, it's just part of the full class name.