我看到有大蝦寫過的一個擴展類,BCL里有Property這個類嗎,求解答。
public static class PropertyExtension
{
public static object GetValueByName(this object self, string propertyName)
{
if (self == null)
{
return self ;
}
Type t = self.GetType();
PropertyInfo p = t.GetProperty(propertyName);
return p.GetValue(self, null);
}
}
5 回答

桃花長相依
TA貢獻1860條經(jīng)驗 獲得超8個贊
@Zery-zhang: 有了PropertyExtension之后,就可以直接這樣用了:string str = "123"; int len = str.GetValueByName("Length");你測試下試試。
- 5 回答
- 0 關(guān)注
- 451 瀏覽
添加回答
舉報
0/150
提交
取消