how to get type of nested class with Type.GetType(string)

后端 未结 1 573
盖世英雄少女心
盖世英雄少女心 2021-01-12 07:56

I can create a new class with a fully qualified name like Namespace.OuterClass.NestedClass. But attempting to get the type with Type.GetType(\"Namespace.

相关标签:
1条回答
  • 2021-01-12 08:49

    String values for C# fully qualified names use + between classes. Get the type by string with Type.GetType("Namespace.OuterClass+NestedClass").

    MSDN documentation for Type.GetType(string) gives a syntax table for various types (generic types, arguments, unmanaged pointers, etc.) including "parent class and a nested class".

    Adding these lines to the question's sample code:

    string typeName1 = typeof(Sample.Program.Vegetable).FullName;
    string typeName2 = typeof(Vegetable).FullName;
    

    will prove the string type name equal to Sample.Program+Vegetable

    ECMA-335 Partition IV's associated CLILibrary.xml provides the definitive details for this convention. The Type.GetType(string) syntax table in ECMA-335 is identical to that used in the MSDN documentation.

    0 讨论(0)
提交回复
热议问题