문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top