由於某些原因,我需要創建一個與某些類的屬性相對應的PropertyInfo
實例字典(我們稱之爲EntityClass
)。我可以使用typeof(EntityClass).GetProperties()
。從屬性中獲取屬性信息,但不會將屬性名稱作爲字符串
但我還需要確定一些特定屬性的值(在編譯時已知)。 通常我可以做下列之一:
EntityInstance.PropertyX = Value;
typeof(EntityClass).GetProperty("PropertyX").SetValue(EntityInstance, Value, null);
爲了填補我的字典,我需要使用,而不僅僅是通常設定值PropertyInfo
實例。但我不覺得舒適的獲取屬性的字符串名稱。如果某些EntityClass發生更改,則會帶來許多異常而不是編譯錯誤。所以,我問的是:
如何獲得已知屬性的PropertyInfo而不傳遞字符串名稱?
SomeDelegateType MyDelegate = EntityInstance.MethodX;
理想:
SomePropertyDelegate MyPropertyDelegate = EntityInstance.PropertyX;
請問你能描述一個場景嗎?我的意思是,修改實體之前和之後的代碼。另外,你怎麼能期望被傳遞的值是修改後屬性的相同類型? –
可能重複[如何獲取特定屬性的PropertyInfo?](http://stackoverflow.com/questions/491429/how-to-get-the-propertyinfo-of-a-specific-property) – nawfal