我在 C# 的代码中有一个类,我想从数组中的嵌套类中获取所有属性,其中包含参数数量的大小以及对象数组中所有参数的内容。像这些:
class MyClass {
class Parameters {
public const string A = "A";
public const string B = "B";
public const string C = "C";
public const string D = "D";
public const string E = "E";
public const string F = "F";
}
public object[] getAllParameters() {
object[] array = new object[6];
array[0] = Parameters.A;
array[1] = Parameters.B;
array[2] = Parameters.C;
array[3] = Parameters.D;
array[4] = Parameters.E;
array[5] = Parameters.F;
}
//more methods and code
}
但是,如果我想添加例如G
和H
参数,我将不得不更新方法的大小getAllParameters
、初始化以及代码其他部分的更多内容。
我可以在getAllParameters
不考虑显式参数的情况下更通用地执行此“”方法吗?也许有反射?