有没有办法用变量引用属性名称?
场景:对象 A 具有公共整数属性 X 和 Z,所以...
public void setProperty(int index, int value)
{
string property = "";
if (index == 1)
{
// set the property X with 'value'
property = "X";
}
else
{
// set the property Z with 'value'
property = "Z";
}
A.{property} = value;
}
这是一个愚蠢的例子,所以请相信,我对此很有用。