2010-10-13 73 views
1

我有一個應用程序,我在哪些方面使用過濾器條件,但我不知道在這個過濾條件中使用字 「遞歸」的含義 這是一些代碼爲什麼我們需要遞歸地過濾內容

  // Indicates a recursive filter. Only valid for object type property filters. 
     private bool m_recursive = false; 
------------------------ 
     /// <summary> 
     /// Method to apply an object filter to an object. 
     /// </summary> 
     /// <param name="myObject">the object to which to apply the filter</param> 
     /// <returns>true if the object passes the filter, false otherwise</returns> 
     public bool Apply(DataModelObject myObject) 
     { 


---------------------- 




     /// Method to apply a property filter 
     /// </summary> 
     /// <param name="myObject">the object to which to apply the filter</param> 
     /// <param name="filteredType">type of the object to which to apply the filter</param> 
     /// <returns>true if the object passes the property filter, false otherwise</returns> 
     public bool Apply(DataModelObject myObject, Type filteredType) 
     { 




switch(FilterType) 
        { 
        case enumFilterType.regularExpr: 
        switch(Operator) 
        { 
         case enumOperator.eq: 

------------------------------------ 
case enumFilterType.strExpr: 
        switch(Operator) 
        { 
         case enumOperator.eq: 
------------------------------------- 
case enumFilterType.objectFilt: 
do 
          { 
           retval = ((ObjectFilter)m_filterValue).Apply(propVal); 
           myObject = propVal; 
           if (m_recursive && retval == false && myObject != null) 
           { 
           propVal = (DataModelObject)prop.GetValue(myObject, null); 
           } 
           else 
           { 
           myObject = null; 
           } 
          } while (myObject != null); 
         } 
         if(m_operator == enumOperator.ne) 
         { 
          retval = !retval; 
         } 
----------------------- 
     public object Clone() 
     { 
     clone.m_recursive = this.m_recursive; 
     return clone; 
     } 

可爲什麼遞歸錯誤使用這裏

+1

請縮進代碼 – 2010-10-13 07:02:52

+0

另外,請讓我們知道您在代碼中顯示的方法的簽名。 – 2010-10-13 07:08:43

+0

我編輯了我的問題 – peter 2010-10-13 07:38:43

回答

2

任何一個可以告訴我你的代碼最重要的部分是這樣的:

do 
{ 
    retval = ((ObjectFilter)m_filterValue).Apply(propVal); 
    myObject = propVal; 
    if (m_recursive && retval == false && myObject != null) 
    { 
    propVal = (DataModelObject)prop.GetValue(myObject, null); 
    } 
    else 
    { 
    myObject = null; 
    } 
} while (myObject != null); 

基本上,當FilterTypeobjectFilt時,代碼將進入do...while循環,該循環是始終至少運行一次的代碼循環,因爲在執行循環代碼後檢查遞歸條件(在此例中爲myObject != null)一旦。

如果m_recursive是假,則retvalmyObject被忽略,myObject設置爲null,所以當被檢查的遞歸條件,它將失敗並退出循環。 myObject被空和retval是假的:

如果m_recursive設置爲true,則設置myObject爲null是由兩個因素決定。

retvalm_filterValue.Apply(propVal)設置。目前尚不清楚propVal從哪裏來。

如果您不知道遞歸是什麼,它就是一段代碼讓自己再次運行的地方。在你的代碼中,這由do...while循環表示。