首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到mscorlib中的类型

找不到mscorlib中的类型
EN

Stack Overflow用户
提问于 2010-08-10 02:53:35
回答 2查看 1.7K关注 0票数 1

有件事我不能理解。我无法读取类型引用:

代码语言:javascript
复制
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;

这怎麽可能?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-10 03:00:35

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

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

演示这一点非常简单:

代码语言:javascript
复制
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(...)不能将它作为一种类型。

票数 3
EN

Stack Overflow用户

发布于 2010-08-10 02:56:57

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3443210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档