C# 打破递归循环来检查条件,这可能吗?

C# 打破递归循环来检查条件,这可能吗?,c#,C#,在这种情况下,我需要使用相同的递归循环,但在循环之前检查不同的条件 这是我的递归方法 private static CategoryFeatureType GetParentCategoryType( DMS.ProductLocation productLocation, DMS.ProductLocation[] productLocations, Dictionary<string, CategoryFeatureType> categoryF

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

这是我的递归方法

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);
        }
    }
}
私有静态CategoryFeatureType GetParentCategoryType(
DMS.ProductLocation ProductLocation,DMS.ProductLocation[]productLocations,
字典类别功能类型,
整数级)
{
CategoryFeatureType CategoryFeatureType=null;
类别特征类型
.TryGetValue(productLocation.ExternalChannelCode,
外类别特征类型);
//这里是我想要返回对象以执行条件检查的地方
//如果需要的话,回到方法中来。
返回categoryFeatureType;
if(conditionTypeCollection==null&&
productLocation.ProductLocationIdForParent.HasValue)
{
DMS.ProductLocation parentLocation=productLocations
.FirstOrDefault(p=>
p、 Id==productLocation.ProductLocationIdForParent);
if(parentLocation!=null)
{
conditionTypeCollection=GetParentCategoryTypeCollection(
父位置,
产品位置,
类别功能类型,
级别+1);
}
}
}
通常,我会在方法内部使用条件检查,但因为它需要在使用不同条件检查的不同情况下使用

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


如果这在C中是可能的,或者我的方法不正确吗?

我认为通过将a作为参数传递给函数,可以实现所描述的功能。

可以让调用方传入一个返回
bool
的委托(比如,)。您的
GetParentCategoryType
方法只需调用委托,并使用其返回的
true
false
值指示是否中断。递归时,可以通过以下方式传递同一委托:

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 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);
}
私有静态CategoryFeatureType GetParentCategoryTypeNeverExit(
DMS.ProductLocation产品位置,
DMS.ProductLocation[]productLocations,
字典类别功能类型,
整数级)
{
谓词returnCheck=categoryFeatureType=>
{
返回false;
};
返回GetParentCategoryType(
产品位置,
产品位置,
类别功能类型,
水平,
退货支票);
}

您可以让调用者传入一个委托,该委托返回一个
bool
(比如,
谓词
)。然后,您可以使用传入预定义委托的方法包装此方法,例如
GetParentCategoryTypeWhereTypeIsAwesome
等等。您的
GetParentCategoryType
将简单地调用委托,并使用其返回的
true
false
值指示是否中断。如果希望在不进行条件检查的情况下执行它,可以检查
null
委托,也可以传入一个始终返回
false
(或
true
的委托,具体取决于您如何框定它)
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);
}