2009-09-03 19 views
1

オブジェクトの子プロパティの問題の値、私はこのコードを持つオブジェクトの子プロパティを取得

PropertyDescriptorCollection childProperties = TypeDescriptor.GetProperties(theObject)childNode.Name] .GetChildProperties()を設定します。

"theObject"変数はTextBoxであり、TextBox.Font.Bold = trueを設定しようとします。

メインプロパティにこのコードを使用し、メインプロパティをカスタマイズすると機能します。しかし、子プロパティにアクセスすると、 "オブジェクト参照がオブジェクトのインスタンスに設定されていません"というエラーが表示されます。

その後、

<TextBox> 
    <Font Bold="true"/> 
</Textbox> 

そして、あなたは、テキストボックスのFontプロパティを取得:それの顔に、それはあなたのような何かを得るように見える - あなたはSetValueに間違ったオブジェクトを渡しているよう

foreach (PropertyDescriptor childProperty in childProperties) 
     { 
      foreach (XmlAttribute attribute in attributes) 
      { 
       if (childProperty.Name == attribute.Name) 
       { 

        if (!childProperty.IsReadOnly) 
        { 

         object convertedPropertyValue = ConverterHelper.ConvertValueForProperty(attribute.Value, childProperty); 

         childProperty.SetValue(theObject, convertedPropertyValue); //exception throw here 

         break; 
        } 
       } 
      } 
     } 
+0

完全な例外を貼り付けることができます –

答えて

2

に見えますフォントのBoldプロパティを次にtrueBoldプロパティにTextBoxに割り当てようとします。明らかに、これは機能しません。

たぶん、このような何か:子オブジェクトのプロパティを設定するためのコンテキストは子オブジェクトではなく、親オブジェクトであることを

PropertyDescriptor objProp = TypeDescriptor.GetProperties(theObject)[childNode.Name]; 
PropertyDescriptorCollection childProperties = objProp.GetChildProperties(); 

foreach (PropertyDescriptor childProperty in childProperties) { 
    foreach (XmlAttribute attribute in attributes) { 
     if (childProperty.Name == attribute.Name && !childProperty.IsReadOnly) { 
      Object convertedPropertyValue = converterHelper.ConvertValueForProperty(attribute.Value, childProperty); 
      childProperty.SetValue(objProp.getValue(theObject), convertedPropertyValue); 
     } 
    } 
} 

は注意してください。

+0

xmlから属性と値を取得できます。そこに問題はありませんが、今私はこのエラーが表示されます: "オブジェクトはターゲットの種類と一致しません。 SetValueメソッドの場合。 – Can

+0

私は間違いを見つけたと思います。私はchildObjectが 'Font'のインスタンスであると仮定しましたが、そうではありません。私は答えを書き直しました、今度はそれを試してみてください。 – Guss

+0

はい。どうもありがとうございます。 – Can

関連する問題