Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 there might be a way using reflection but haven't been able to find one.

like image 745
Dr. Thomas C. King Avatar asked Apr 06 '11 12:04

Dr. Thomas C. King


People also ask

Are inner classes public?

Java inner class is defined inside the body of another class. Java inner class can be declared private, public, protected, or with default access whereas an outer class can have only public or default access.

Can we inherit inner class?

Inner classesA inner class declared in the same outer class (or in its descendant) can inherit another inner class.


2 Answers

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

like image 105
Chris Pitman Avatar answered Sep 28 '22 02:09

Chris Pitman


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.

like image 41
Jon Skeet Avatar answered Sep 28 '22 01:09

Jon Skeet