2012-08-02 64 views
1

我有一個對象,我已經定義了,我希望使用PropertyDescriptor修改它的一個名爲「DeviceType」的屬性,但它不適合我。PropertyDescriptor - SetValue()異常,而GetValue()工作

我可以檢索屬性DeviceType的值,但是當我使用SetValue()更改它的屬性值時,我總是得到異常:即使我嘗試設置「對象引用未設置爲對象的實例」該屬性的值與我剛剛檢索的值相同。

下面是一些代碼:

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(theBase); 
var objValue = properties["DeviceType"].GetValue(theBase); 

try 
{ 
    if (theBase != null && objValue != null) 
    { 
     properties["DeviceType"].SetValue(theBase, objValue); 
    } 
} 
catch (Exception ex) 
{ 

} 

任何幫助將非常感激。 謝謝。

+0

該屬性是否有一個setter? – leppie 2012-08-02 01:06:34

+0

可能的空引用異常來自DeviceType設置器 – cordialgerm 2012-08-02 01:43:17

+0

@leppie:感謝您的迴應。是的,該物業確實有一個設置。 – G24T 2012-08-02 03:36:55

回答

1

當我的對象被創建時,屬性沒有被正確初始化,所以setter屬性產生一個空引用異常錯誤。

相關問題