0

Is it possible to get a list of class name of Parent and list of the field names in class ChildA:

I created this class to define the configuration codes (because I don't want to use string, it is not stable and difficult to manage):

public class CodeConfiguration
    {
        public class ModuleA
        {
            public const string GroupA = "ProvinCode";
            public const string GroupB = "CountryCode";
        }
        public class ModuleB
        {
            public const string GroupC = "ImportStatus;
            public const string GroupD = "ExecuteStatus";
        }
    }

In database, my data table is as below

Module  Group   Code    Name
ModuleA GroupA  1       100
ModuleA GroupB  2       1001
ModuleB GroupC  1       Imported
ModuleB GroupD  2       Failed

So now I need to get data for Module Combobox and Group Combobox My expected result:

Module Combobox: {"ModuleA","ModuleB} Group Combobox of Module A: {"GroupA","GroupB"}

Hoang Tran
  • 886
  • 3
  • 13
  • 32
  • Short answer: Yes. – H H Oct 11 '17 at 04:25
  • 4
    But you should not want this. For one thing, public nested classes are discouraged. – H H Oct 11 '17 at 04:26
  • Note that you asked "list of the *variable names* in class ChildA" but did not show any... Are you sure you've used proper term for what you are looking for? – Alexei Levenkov Oct 11 '17 at 04:36
  • I have updated my question to show my expected result. – Hoang Tran Oct 11 '17 at 04:43
  • Those are not called variables, those are called fields, – Scott Chamberlain Oct 11 '17 at 04:46
  • I will slightly add to Henk's argument: Public nested classes are ***strongly*** discouraged. --- There are no good reasons or use-cases for doing it, as far as my experience goes; and most, if not all, of the developers I know seem to agree. --- @ScottChamberlain It's not *technically* accurate, I agree; but in real-world practice, most people use `variable` and `field` synonymously and even interchangeably (?). [insert shrug here] – CosmicGiant Oct 11 '17 at 04:52
  • @HoangTran That questions looks to me as if it's not the actual question you should ask. I say that because your goal is most likely not to do what you're asking, but you're trying to do what your asking in order to achieve something else. – Tipx Oct 11 '17 at 05:05
  • @Tipx: I have updated my question to make it more cleary – Hoang Tran Oct 11 '17 at 06:00

1 Answers1

1

You can get list of nested class names with:

var nestedClasses = typeof(CodeConfiguration)
    .GetNestedTypes()
    .Select(_ => _.Name)
    .ToList();

If you want to include private nested classes, pass BindingFlags.Public | BindingFlags.NonPublic as an argument to .GetNestedTypes() method.

Get list of field names with:

var fieldNames = typeof(CodeConfiguration.ModuleA)
    .GetFields()
    .Select(_ => _.Name)
    .ToList();

To include private fields, pass BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance as an argument to .GetFields() method.


See also Reflection in the .NET Framework
Hoang Tran
  • 886
  • 3
  • 13
  • 32
Ňuf
  • 6,027
  • 2
  • 23
  • 26
  • Thank bro, I have updated the class names in my question so I updated them in your answer also – Hoang Tran Oct 11 '17 at 06:09
  • Hi bro, instead of typeof(CodeConfiguration.ModuleA), Is it possible to use a string name like typeof("ModuleA") ? – Hoang Tran Oct 11 '17 at 07:15
  • `typeof` doesn't support string as an argument, but you can use `Type.GetType(name)` or `Assembly.GetType(name)` instead. – Ňuf Oct 11 '17 at 07:29
  • I tried Type.GetType("MyNamespace.CodeConfiguration.ModuleA") but it was null. – Hoang Tran Oct 11 '17 at 07:40
  • unlike `Assembly,GetType()`, argument of `Type.GetType()` must be Assembly qualified name - see [docs](https://learn.microsoft.com/en-us/dotnet/api/system.type.gettype?view=netframework-4.7#System_Type_GetType_System_String_) – Ňuf Oct 11 '17 at 07:48