Is there a way to get a list of innerclasses in C#?

后端 未结 5 1202
天命终不由人
天命终不由人 2020-12-07 00:25

As per the title. I\'d like a list of all the inner classes of a given class, it can be a list of names or a list of types - I am not fussed. Is this possible? I thought the

相关标签:
5条回答
  • 2020-12-07 00:57

    Doesn't Type.GetNestedTypes do what you want?

    Note that if you want to get "double-nested" types, you'll need to recurse - as Foo.Bar.Baz is a nested type in Foo.Bar, not in Foo.

    For "modern" environments (.NET 4.5, PCLs, UWA etc) you need TypeInfo.DeclaredNestedTypes instead, e.g. type.GetTypeInfo().DeclaredNestedTypes, using the GetTypeInfo() extension method.

    0 讨论(0)
  • 2020-12-07 00:58

    Type.GetNestedTypes() will return the public nested types of the specified Type.

    If you also want the private and internal nested types, you must call the Type.GetNestedTypes(BindingFlags bindingFlags) method like this:

    Type[] nestedTypes = typeof(MyType).GetNestedTypes(BindingFlags.Static |
                                                       BindingFlags.Instance |
                                                       BindingFlags.Public |
                                                       BindingFlags.NonPublic);
    
    0 讨论(0)
  • 2020-12-07 00:59

    Yes, there is. Use Type.GetNestedTypes().

    0 讨论(0)
  • 2020-12-07 01:00
    Type[] nested = typeof(SomeClass).GetNestedTypes();
    
    0 讨论(0)
  • 2020-12-07 01:12

    You want Type.GetNestedTypes. This will give you the list of types, which you can then query for their names.

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