有件事我不能理解。我无法读取类型引用:
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;这怎麽可能?
发布于 2010-08-10 03:00:35
最后一行不能编译的原因是因为IDefinitionAppId是内部的,而不是因为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(...)不能将它作为一种类型。
发布于 2010-08-10 02:56:57
System.Deployment.Internal.Isolation是一个命名空间,不是一个类型,你不能得到一个命名空间的“引用”,它只是整个类名的一部分。
https://stackoverflow.com/questions/3443210
复制相似问题