2013-10-19 51 views
0

這是寫在C#的代碼段,我被卡住[編輯]:操作「==」不能應用於操作數System.Reflection.PropertyInfo和「廉政」

foreach (var property in this.allProperties) 
     { 
      var propertyItself = element.GetType().GetProperty(property.GetType().Name); 

      if (propertyItself.PropertyType != typeof(Int32)) 
      { continue; } 

      if (propertyType == 0) 
      { return false; } 
     } 

如果任何一個除此之外,可以幫助提供一些信息?提前致謝!

+1

您需要在評估屬性的_value_之前調用'GetValue'。 –

+1

你想用這段代碼做什麼? –

+3

爲什麼'!(x!= y)'?不會'x == y'更容易遵循嗎? –

回答

3

你需要一個像評估的財產前致電PropertyInfo.GetValue方法,以便

if (propertyItself == typeof (Int32)) 
{ 
    if((int) propertyItself.GetValue(element) == 0) 
    { 
     return false; 
    } 
} 

您也可以考慮提高你表達的可讀性,因爲我已經通過評估上面做類型等於一個整數。

+1

,我不知道這太'var propertyItself = element.GetType()。GetProperty(property.GetType()。Name);' –

+0

@ByteBlast,在那個改變之後,現在我得到一條消息:「方法'GetValue'沒有超載需要1個'參數',這是什麼意思?你怎麼看?即使認爲這種情況正在發生,我認爲這仍然是一個很好的方法 –

+0

這意味着它需要比你指定的參數更多的參數 –

相關問題