请问要如何从 method 去带入 class 的属性?
如以下范例
感谢~
class person
{
public string weight = "50";
public string height = "150";
}
class Program
{
// 由 item 可以选择 weight或是 height
public void printItemValue(person p, string item)
{
Console.WriteLine(p.item);
}
static void Main(string[] args)
{
person A = new person();
person B = new person();
person C = new person();
person D = new person();
List<person> persons = new List<person>() { A , B, C, D};
foreach (person p in persons)
{
printItemValue(p, "weight");
}
}
}