2010-10-13 71 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循环表示。