0

I wanna know how to count number of global variable exist in class which i called.

I used parameter with Several types of inherited classes, which are different number of variable.

And if possible i want to get name of variable..

public class MotherClass
{
    public int motherAge;
}

public class ChildClass : MotherClass {
    public int childAge;
    public string childName;
}

public void Main(MotherClass person) { 
    //count number of variable exist in "person"
    //get name of variables exist in "person"
}
1

1 Answer 1

2

I assume by "global variable in class" you mean public field like public int motherAge;

using System.Reflection;
using System.Linq;
MotherClass person = new MotherClass();
var fields = person.GetType().GetFields().Where(i => i.IsPublic);
// Do something with fields.Count

foreach (FieldInfo item in fields)
{
  // Do something with item.Name;
}


0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.