我在這裏簡單地提出了這個問題,因此該示例無法爲真實世界做出任何貢獻。無法通過反射獲取泛型類型的屬性
public class BusinessEntity<T>
{
public int Id {get; set;}
}
public class Customer : BusinessEntity<Customer>
{
public string FirstName { get; set;}
public string LastName { get; set;}
}
當我嘗試通過反射獲取Customer類屬性時,無法獲取泛型基類的屬性。如何從BusinessEntity獲取ID?
Type type = typeof(Customer);
PropertyInfo[] properties = type.GetProperties();
// Just FirstName and LastName listed here. I also need Id here
剛剛測試過這種情況,我返回的屬性數組總是有3項(VS2012,嘗試多個目標框架)。 – Timbo 2012-07-25 07:58:56
「如何從BusinessEntity獲取Id?」去洗你的眼睛? :p – 2012-07-25 08:00:25
這裏有個訣竅:當你簡化問題的代碼時,*檢查它是否仍然顯示問題*。如果它沒有**顯示問題,那麼看看真實代碼和簡化代碼之間的區別,然後你自己回答*。 – 2012-07-25 08:03:10