未找到 mscorlib 中的类型

发布于 2024-09-13 04:54:51 字数 452 浏览 3 评论 0原文

有件事我无法理解。我无法阅读类型参考:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖微凉心微凉 2024-09-20 04:54:51

最后一行无法编译的原因是 IDefinitionAppIdinternal - 而不是因为 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(...) 不这样做找到它作为一种类型。

The reason your last line won't compile is because IDefinitionAppId is internal - not because System.Deployment.Internal.Isolation is a type.

Note that if Isolation were the name of a type, you'd have to use GetType("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:

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);
    }
}

So System.Deployment.Internal.Isolation is a namespace, not a type, hence why Assembly.GetType(...) doesn't find it as a type.

扭转时空 2024-09-20 04:54:51

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文