views:

67

answers:

2

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?

+1  A: 

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.

Matias Valdenegro
nope, IntelliSense does't see this namespace, hence I cannot reach the nested type IDefinitionAppId
MarcAndreson
If it was a namespace I would be able to have a variable of type System.Deployment.Internal.Isolation.IDefinitionAppId, as you can see in the last line
MarcAndreson
@Marc: IDefinitionAppId isn't a nested type to start with.
Jon Skeet
+3  A: 

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.

Jon Skeet
internal... I knew I had missed something. Thanks.
MarcAndreson