打破递归循环来检查条件,这是可能的吗

本文关键字:条件 递归 循环 检查 | 更新日期: 2023-09-27 17:58:07

我有一种情况,需要使用相同的递归循环,但在循环之前检查不同的条件。

这是我的递归方法;

private static CategoryFeatureType GetParentCategoryType(
        DMS.ProductLocation productLocation, DMS.ProductLocation[] productLocations, 
        Dictionary<string, CategoryFeatureType> categoryFeatureTypes, 
        int level)
{
    CategoryFeatureType categoryFeatureType = null;
    categoryFeatureTypes
            .TryGetValue(productLocation.ExternalChannelCode, 
                         out categoryFeatureType);
    // Here is where i want to return the object to perform a conditional check
    // and come back into the method if needed.
    return categoryFeatureType;
    if (conditionTypeCollection == null && 
        productLocation.ProductLocationIdForParent.HasValue)
    {
        DMS.ProductLocation parentLocation = productLocations
            .FirstOrDefault(p => 
                p.Id == productLocation.ProductLocationIdForParent);
        if (parentLocation != null)
        {
            conditionTypeCollection = GetParentCategoryTypeCollection(
                                          parentLocation, 
                                          productLocations, 
                                          categoryFeatureTypes, 
                                          level + 1);
        }
    }
}

通常,我会在方法内部使用条件检查,但由于它需要在不同的情况下使用,因此会使用不同的条件检查。

我可以为每个条件检查创建一个方法,但我不想这样做。

如果这在C#中是可能的,或者我是不是用了一种不正确的方式?

打破递归循环来检查条件,这是可能的吗

您可以让调用者传入一个返回bool(比如Predicate<CategoryFeatureType>)的委托。GetParentCategoryType方法将简单地调用委托,并使用其返回的truefalse值来决定是否中断。当递归时,您可以将相同的委托传递通过:

private static CategoryFeatureType GetParentCategoryType(
    DMS.ProductLocation productLocation, 
    DMS.ProductLocation[] productLocations, 
    Dictionary<string, CategoryFeatureType> categoryFeatureTypes, 
    int level, 
    Predicate<CategoryFeatureType> shouldBreak)
{
   CategoryFeatureType categoryFeatureType = null;
   categoryFeatureTypes.TryGetValue(productLocation.ExternalChannelCode, out categoryFeatureType);
   if (shouldBreak(categoryFeatureType))   
        return categoryFeatureType;
   if (conditionTypeCollection == null && productLocation.ProductLocationIdForParent.HasValue)
   {
       DMS.ProductLocation parentLocation = productLocations.FirstOrDefault(p => p.Id == productLocation.ProductLocationIdForParent);
       if (parentLocation != null)
       {
           conditionTypeCollection = GetParentCategoryTypeCollection(
            parentLocation, 
            productLocations, 
            categoryFeatureTypes, 
            level + 1, 
            shouldBreak);
       }
   }
}

然后,您可以使用传入预定义委托的方法来包装此方法:

private static CategoryFeatureType GetParentCategoryTypeWhereTypeIsAwesome(
    DMS.ProductLocation productLocation, 
    DMS.ProductLocation[] productLocations, 
    Dictionary<string, CategoryFeatureType> categoryFeatureTypes, 
    int level)
{
    Predicate<CategoryFeatureType> returnCheck = categoryFeatureType =>
    {
        return categoryFeatureType.Coolness == "Awesome";
    };
    return GetParentCategoryType(
        productLocation, 
        productLocations, 
        categoryFeatureTypes, 
        level, 
        returnCheck);
}

如果你想在没有条件检查的情况下执行它,你可以检查一个空委托,或者你可以传入一个总是返回false:的委托

if (shouldBreak != null && shouldBreak(categoryFeatureType))   
    return categoryFeatureType;

或者使用空包装:

private static CategoryFeatureType GetParentCategoryTypeNeverExit(
    DMS.ProductLocation productLocation, 
    DMS.ProductLocation[] productLocations, 
    Dictionary<string, CategoryFeatureType> categoryFeatureTypes, 
    int level)
{
    Predicate<CategoryFeatureType> returnCheck = categoryFeatureType =>
    {
        return false;
    };
    return GetParentCategoryType(
        productLocation, 
        productLocations, 
        categoryFeatureTypes, 
        level, 
        returnCheck);
}

我认为您可以通过将谓词作为参数传递给函数来实现所描述的内容。