可以通過索引訪問屬性嗎?C#類屬性,按索引獲取它們
以類Person爲例,它可能具有屬性「BirthNo」和「Gender」。如果我想訪問BirthNo的價值,是否有可能以任何方式編寫p。[0] .Value還是必須寫person.BirthNo.Value?
Person p = new Person
//I have this:
string birthNo = p.BirthNo.Value;
//I want this:
string birthNo = p.[0].Value;
他們**不屬性**,但**屬性**。要通過索引得到它們,你必須在僞代碼中使用** Reflection **:'string birthNo =(string)p.GetType()。GetProperties()[0] .GetValue(p,null);'。 – 2013-04-09 18:06:54
否;你爲什麼想要?我懷疑有一種更好的方法可以做到你想做的事情,而不需要通過索引訪問屬性。 – 2013-04-09 18:09:07
@Adriano你應該把你的評論作爲回答 – Alex 2013-04-09 18:09:09