为什么我会收到ReSharper错误“提取的代码具有多个入口点”?

16

我正在使用ReSharper来重构我的代码。当我尝试将一段代码块移动到方法中时,我会收到以下警告:

提取的代码有多个入口点

这是我打算使用的方法签名:

private void GetRatePlanComponents(ProductPlan productPlan, 
    ProductRatePlan productRatePlan)    

我在网上搜索了解它的含义,但并没有太多的帮助。有人可以解释一下吗?

供参考,这里是我想要移动到独立方法的代码片段:

QueryResult productRatePlanChargeQueryResult = 
    _zuoraService.query(string.Format(@"select Id, Name, IncludedUnits from
        ProductRatePlanCharge where ProductRatePlanId = '{0}' and 
        ChargeModel = 'Overage Pricing'", productRatePlan.Id));

if (productRatePlanChargeQueryResult.size > 0)
{
    foreach (ProductRatePlanCharge productRatePlanCharge 
        in productRatePlanChargeQueryResult.records)
    {
        string numberOfUnits = productRatePlanCharge.IncludedUnits.ToString();

        if (productRatePlanCharge.Name.Equals("Users"))
        {
            productPlan.NumberofUsers = numberOfUnits;
        }
        else if (productRatePlanCharge.Name.Equals("Projects"))
        {
            productPlan.NumberofProjects = numberOfUnits;
        }
        else if (productRatePlanCharge.Name.Equals("Storage"))
        {
            decimal volumeOfStorage;
            if (decimal.TryParse(productRatePlanCharge.IncludedUnits.ToString(), 
                out volumeOfStorage))
            {
                if (volumeOfStorage < 1) volumeOfStorage *= 1000;
                    productPlan.VolumeofStorage = volumeOfStorage.ToString();
                }
                else
                {
                    productPlan.VolumeofStorage = numberOfUnits;
                }
            }
        }
    }
}

3
你确定它说的是多个“入口点”而不是“出口点”吗?它指向特定的那一行吗?这就是整个方法吗?你能包括方法签名吗? - Jon Skeet
@Jon Skeet:是的,它说“入口点”。请看更新后的问题。 - Moon
2个回答

7

看起来你可能遇到了已知问题

public static IEnumerable<ITagPrefixHolder> GetRelevantHolders(IPsiSourceFile sourceFile )
{
  var targetPath = FileSystemPath.Empty;
  var projectFile = sourceFile.ToProjectFile();
  if (projectFile != null)
    targetPath = projectFile.Location;

  foreach(var holder in GetRelevantHoldersBeforeFile(sourceFile, targetPath))
    yield return holder;

  foreach(var holder in GetHoldersInFile(sourceFile, targetPath))
    yield return holder;
}

选择两个foreach循环并提取方法。它会给出奇怪的警告,即该片段有多个入口点(??!)并导致以下代码:

public static IEnumerable<ITagPrefixHolder> GetRelevantHolders(IPsiSourceFile sourceFile )
{
  var targetPath = FileSystemPath.Empty;
  var projectFile = sourceFile.ToProjectFile();
  if (projectFile != null)
    targetPath = projectFile.Location;

  foreach(var tagPrefixHolder in Foo(sourceFile, targetPath))
       yield return tagPrefixHolder;
}

private static IEnumerable<ITagPrefixHolder> Foo(IPsiSourceFile sourceFile, FileSystemPath targetPath)
{
  foreach(var holder in GetRelevantHoldersBeforeFile(sourceFile, targetPath))
    yield return holder;
  foreach(var holder in GetHoldersInFile(sourceFile, targetPath))
    yield return holder;
}
最好用简单的 return Foo(sourceFile, targetPath); 替换生成的 foreach 循环。

1

当我尝试提取的代码中有几个throw语句时,我曾见过ReSharper做同样的事情。

你可以像我那样逐行注释,直到找到ReSharper提示错误的那一行。然后你可以提取方法并在之后取消注释。

或者你也可以手动重构它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接