为什么我们需要递归过滤内容

时间:2010-10-13 06:59:45

标签: c# winforms

我有一个应用程序,其中一些方面我使用过滤条件,但我不知道使用word的含义 在过滤条件下“递归” 这是一些代码

          // 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;
      }

任何人都可以告诉我为什么recursive false在这里使用

1 个答案:

答案 0 :(得分: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为false,则忽略retvalmyObject并将myObject设置为null,因此当检查递归条件时,它会失败并且循环退出

如果m_recursive设置为true,则设置myObject为空由两件事决定:myObject为空,retval为假。

retvalm_filterValue.Apply(propVal)设置。目前还不清楚propVal的来源。

如果您不知道递归是什么,那么它就是一段代码导致其自身再次运行的地方。在您的代码中,这由do...while循环表示。