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

Moo*_*oon 16 .net c# resharper refactoring c#-3.0

我正在使用ReSharper来重新计算代码.当我尝试将一段代码移动到该方法时,我收到以下警告:

The extracted code has multiple entry points

这是我计划使用的方法签名:

private void GetRatePlanComponents(ProductPlan productPlan, 
    ProductRatePlan productRatePlan)    
Run Code Online (Sandbox Code Playgroud)

我在网上搜索了解它的含义.但没有太多运气.有人解释一下吗?

供您参考,以下是我尝试转移到单独方法的代码段:

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;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Aak*_*shM 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;
}
Run Code Online (Sandbox Code Playgroud)

选择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;
}
Run Code Online (Sandbox Code Playgroud)

用简单的方法替换生成的foreach会更好return Foo(sourceFile, targetPath);.