如何使用Type.GetType(string)获取嵌套类的类型

jlt*_*rem 6 c# syntax

我可以创建一个具有完全限定名称的新类Namespace.OuterClass.NestedClass.但试图获得Type.GetType("Namespace.OuterClass.NestedClass")返回类型null.这是示例代码:

namespace Sample
{
   public class Program
   {
      public class Animal { }
      public class Vegetable { }
      public class Mineral { }

      static public void Test()
      {
         Object o = new Sample.Program.Vegetable();
         Type t = Type.GetType("Sample.Program.Vegetable"); // returns null
         Console.ReadKey();
      }

      static void Main(string[] args)
      {
         Program.Test();
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我如何Type.GetType用于嵌套类?

jlt*_*rem 19

C#完全限定名称的字符串值+在类之间使用.通过字符串获取类型Type.GetType("Namespace.OuterClass+NestedClass").

MSDN文档Type.GetType(string)提供了各种类型(泛型类型,参数,非托管指针等)的语法表,包括"父类和嵌套类".

将这些行添加到问题的示例代码中:

string typeName1 = typeof(Sample.Program.Vegetable).FullName;
string typeName2 = typeof(Vegetable).FullName;
Run Code Online (Sandbox Code Playgroud)

将证明字符串类型名称等于 Sample.Program+Vegetable

ECMA-335分区IV的关联CLILibrary.xml为此约定提供了明确的详细信息.Type.GetType(string)ECMA-335中的语法表与MSDN文档中使用的语法表相同.