Recursive selection of new overload forms of the extension method (recursionselect)

Source: Internet
Author: User

Recursionselect is an extremely convenient extension method, which requires only a fewCodeThis extension method is frequently used in my code. This is an example I have provided before:

[Testmethod] public void testmethod13 () {// obtain the directory set var d = new directoryinfo (@ "C: \ Users \ public \ downloads") of all subdirectories in the specified directory "); vaR c = D. recursionselect (F => F. getdirectories (), F => F. getdirectories (). length> 0); MessageBox. show (C. count (). tostring ());}

This method originated from the article by crane chongtian.Article: Http://www.cnblogs.com/ldp615/archive/2009/11/09/1599312.html

I have shared in my article my own implementation version (recursionselect): http://www.cnblogs.com/SkyD/archive/2010/01/15/1648178.html

Now, I have added a small improvement to it. Through the new method of reload, the judgment expression can be used to abort recursion, which makes it much more flexible.

Specifically, a Boolean parameter named "check failed or not" is added. The default execution method is "true ", that is, "The following recursion will continue even if the judgment fails", so that the judgment expression can only serve as a filter (similar to where in LINQ), and if it is set to false, this can block all subsequent operations when the first failure occurs, which is useful in many cases (for example, obtaining continuous object chains with common features ).

The complete code is as follows (the new heavy load and bold display ):

/// <Summary>
/// Loop all subitems, recursively select and return all descendant items, for ienumerable
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "recursive item selection expression"> use this expression to select the child item to return </param>
/// <Returns> selected subitem </returns>
Public static ienumerable <t> recursioneachselect <t> (this ienumerable o, func <t, ienumerable <t> recursive item selection expression)
{
Return recursioneachselect (O, recursive item selection expression, null );
}

///


// loop all subitems, recursively select and return all descendant items, for ienumerable
///
/// type
/// use this expression to select the child item to return
// evaluate the return item for test, returns the item that is created after the expression is substituted
// selected subitem
Public static ienumerable recursioneachselect (this ienumerable O, func recursive item selection expression, predicate test expression)
{< br> return recursioneachselect (O. cast (), recursive item selection expression, test expression);
}

///


// loop all subitems, recursively select and return all descendant items, for ienumerable generic form
///
/// type
/// use this expression to select the child item to be returned
// selected child item
Public static ienumerable recursioneachselect (this ienumerable O, func recursive item selection expression)
{< br> return recursioneachselect (O, recursive item selection expression, null );
}

/// <Summary>
/// Loop all subitems, recursively select and return all descendant items, in the generic form of ienumerable
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "recursive item selection expression"> use this expression to select the child item to return </param>
/// <Param name = ""> evaluate returns the items that are valid after the expression is substituted. </param>
/// <Returns> selected subitem </returns>
Public static ienumerable <t> recursioneachselect <t> (this ienumerable <t> O, func <t, ienumerable <t> recursive item selection expression, predicate <t> test expression)
{
Return recursioneachselect (O, recursive item selection expression, test expression, true );
}

/// <Summary>
/// Loop all subitems, recursively select and return all descendant items, in the generic form of ienumerable
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "recursive item selection expression"> use this expression to select the child item to return </param>
/// <Param name = ""> evaluate returns the items that are valid after the expression is substituted. </param>
/// <Param name = "check whether the expression fails to continue"> indicates whether to recursively select the next item after the expression test fails, the default value is true if this parameter is not specified. </param>
/// <Returns> selected subitem </returns>
Public static ienumerable <t> recursioneachselect <t> (this ienumerable <t> O, func <t, ienumerable <t> recursive item selection expression, predicate <t> test expression, bool checks whether the failure persists)
{
Foreach (var f in O)
{
Bool? B = test expression = NULL? (Bool ?) Null: Test expression (f );
If (B = NULL | B. Value) yield return F;
Else if (! Check whether the failure persists)
{
Yield break;
}
Foreach (var d in recursionselect (F, recursive item selection expression, test expression ))
{
Yield return D;
}
}
}

/// <Summary>
/// Recursively select and return descendant items
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "recursive item selection expression"> use this expression to select the child item to return </param>
/// <Returns> selected subitem </returns>
Public static ienumerable <t> recursionselect <t> (this t o, func <t, t> recursive item selection expression)
{
Return recursionselect (O, recursive item selection expression, null );
}

/// <Summary>
/// Recursively select and return descendant items
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "recursive item selection expression"> use this expression to select the child item to return </param>
/// <Param name = ""> evaluate returns the items that are valid after the expression is substituted. </param>
/// <Returns> selected subitem </returns>
Public static ienumerable <t> recursionselect <t> (this t o, func <t, t> recursive item selection expression, predicate <t> test expression)
{
Return recursionselect (O, recursive item selection expression, test expression, true );
}

/// <Summary>
/// Recursively select and return descendant items
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "recursive item selection expression"> use this expression to select the child item to return </param>
/// <Param name = ""> evaluate returns the items that are valid after the expression is substituted. </param>
/// <Param name = "check whether the expression fails to continue"> indicates whether to recursively select the next item after the expression test fails, the default value is true if this parameter is not specified. </param>
/// <Returns> selected subitem </returns>
Public static ienumerable <t> recursionselect <t> (this t o, func <t, t> recursive item selection expression, predicate <t> test expression, bool check whether failure continues)
{
If (O = NULL) yield break;
VaR F = recursive selection expression (O );
Bool? B = test expression = NULL? (Bool ?) Null: Test expression (f );
If (B = NULL | B. Value) yield return F;
Else if (! Check whether the failure persists)
{
Yield break;
}
Foreach (var d in recursionselect (F, recursive item selection expression, test expression, check whether the failure continues ))
{
Yield return D;
}
}

/// <Summary>
/// Recursively select and return descendant items
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "recursive item selection expression"> use this expression to select the child item to return </param>
/// <Returns> selected subitem </returns>
Public static ienumerable <t> recursionselect <t> (this t o, func <t, ienumerable <t> recursive item selection expression)
{
Return recursionselect (O, recursive item selection expression, null );
}

/// <Summary>
/// Recursively select and return descendant items
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "recursive item selection expression"> use this expression to select the child item to return </param>
/// <Param name = ""> evaluate returns the items that are valid after the expression is substituted. </param>
/// <Returns> selected subitem </returns>
Public static ienumerable <t> recursionselect <t> (this t o, func <t, ienumerable <t> recursive item selection expression, predicate <t> test expression)
{
Return recursionselect (O, recursive item selection expression, test expression, true );
}

/// <Summary>
/// Recursively select and return descendant items
/// </Summary>
/// <Typeparam name = "T"> type </typeparam>
/// <Param name = "recursive item selection expression"> use this expression to select the child item to return </param>
/// <Param name = ""> evaluate returns the items that are valid after the expression is substituted. </param>
/// <Param name = "check whether the expression fails to continue"> indicates whether to recursively select the next item after the expression test fails, the default value is true if this parameter is not specified. </param>
/// <Returns> selected subitem </returns>
Public static ienumerable <t> recursionselect <t> (this t o, func <t, ienumerable <t> recursive item selection expression, predicate <t> test expression, bool checks whether the failure persists)
{
Foreach (var f in recursive item selection expression (o ))
{
Bool? B = test expression = NULL? (Bool ?) Null: Test expression (f );
If (B = NULL | B. Value) yield return F;
Else if (! Check whether the failure persists)
{
Yield break;
}
Foreach (var d in recursionselect (F, recursive item selection expression, test expression ))
{
Yield return D;
}
}
}

 

Download

XPS version: http://www.uushare.com/user/icesee/file/3504904 in this article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.