我有一个应用程序,其中一些方面我使用过滤条件,但我不知道使用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在这里使用
答案 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);
基本上,当FilterType
为objectFilt
时,代码进入do...while
循环,这是一个始终至少运行一次的代码循环,因为递归条件(在在循环代码执行一次后检查这种情况,myObject != null
)。
如果m_recursive
为false,则忽略retval
和myObject
并将myObject
设置为null,因此当检查递归条件时,它会失败并且循环退出
如果m_recursive
设置为true,则设置myObject
为空由两件事决定:myObject
为空,retval
为假。
retval
由m_filterValue.Apply(propVal)
设置。目前还不清楚propVal的来源。
如果您不知道递归是什么,那么它就是一段代码导致其自身再次运行的地方。在您的代码中,这由do...while
循环表示。