2017-02-28 62 views
0

我有一個包含500個參數的對象,我無法更改它,因爲它是共享項目。 我的目標是通過反思獲得財產價值,但我做不到。從共享/ pcl項目中的對象獲取屬性

這裏是我的代碼:

public class MyObjects 
    {  
    public int RGP_Id { get; set; } 
    public DateTime RGP_DateTime { get; set; } 
    public int RGP_MCC_Numero_Serie_MS { get; set; } 
    public int RGP_IDREG_1 { get; set; } 
    public int RGP_IDREG_2 { get; set; } 
    public int RGP_IDREG_3 { get; set; } 
    public int RGP_IDREG_4 { get; set; } 
    public int RGP_IDREG_5 { get; set; } 
    public int RGP_IDREG_6 { get; set; } 
    public int RGP_IDREG_7 { get; set; } 
    public int RGP_IDREG_8 { get; set; } 
    public int RGP_IDREG_9 { get; set; } 
    public int RGP_IDREG_10 { get; set; } 
    ....... 
    public int RGP_IDREG_500 { get; set; } 

} 

...

var profile = MyObjects; 
var idProperty = GetProperty(profile.GetType().GetTypeInfo(), "RGP_IDREG_10"); 

...

idProperty包含對象,並在那裏我做到了。

現在,我該如何獲得使用idProperty的值? 基本上我會得到:

var x = MyObjects.idProperty; 

如何我能得到這個做什麼?

回答

1

假設idPropertyPropertyInfo對象,你可以這樣做:

var result = (int) idProperty.GetValue(profile,null); 

您必須確保轉換該值,因爲它返回爲一個object