【发布时间】:2020-08-15 23:06:04
【问题描述】:
我想知道如何计算我调用的类中存在的全局变量的数量。
我将参数与几种类型的继承类一起使用,它们是不同数量的变量。
如果可能的话,我想获取变量的名称..
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"
}
【问题讨论】:
-
你必须使用反射。请参阅How can I find all the public fields of an object in C#? 和许多其他示例。
标签: c# class global-variables