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

发布于 2024-12-08 05:20:18 字数 1679 浏览 0 评论 0原文

我正在使用 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;
                }
            }
        }
    }
}

I am using the ReSharper to re-factor my code. When I try to move a block of code to the method, I get the following warning:

The extracted code has multiple entry points

Here is the method signature I am planning to use:

private void GetRatePlanComponents(ProductPlan productPlan, 
    ProductRatePlan productRatePlan)    

I searched the web to understand what does it mean. But didn't have much luck. Would someone explain it?

For your reference, here is the code snippet I am trying to move to a separate method:

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;
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怂人 2024-12-15 05:20:18

您可能遇到了已知问题

public static IEnumerable; GetRelevantHolders(IPsiSourceFile 源文件)
{
  var targetPath = FileSystemPath.Empty;
  var projectFile = sourceFile.ToProjectFile();
  if (项目文件!= null)
    targetPath = 项目文件.Location;

  foreach(GetRelevantHoldersBeforeFile(sourceFile,targetPath)中的varholder)
    收益回报持有者;

  foreach(GetHoldersInFile(sourceFile,targetPath)中的varholder)
    收益回报持有者;
}

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

public static IEnumerable; GetRelevantHolders(IPsiSourceFile 源文件)
{
  var targetPath = FileSystemPath.Empty;
  var projectFile = sourceFile.ToProjectFile();
  if (projectFile != null)
    targetPath = 项目文件.Location;

  foreach(Foo 中的 var tagPrefixHolder(sourceFile,targetPath))
       产量返回tagPrefixHolder;
}

私有静态 IEnumerable Foo(IPsiSourceFile 源文件,FileSystemPath 目标路径)
{
  foreach(GetRelevantHoldersBeforeFile(sourceFile,targetPath)中的varholder)
    收益回报持有者;
  foreach(GetHoldersInFile(sourceFile,targetPath)中的varholder)
    收益回报持有者;
}

最好用简单的 return 替换生成的 foreach
Foo(源文件,目标路径);

It looks like you may have encountered a known issue:

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;
}

Select both foreach-loops and extract method. It gives strange warning
that the fragment has multiple entry points (??!) and results in the
following code:

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;
}

It would be better to replace generated foreach with simple return
Foo(sourceFile, targetPath);
.

深居我梦 2024-12-15 05:20:18

当我试图提取的代码有几个 throw 语句时,我看到 ReSharper 做了同样的事情。

您可以像我在这种情况下所做的那样 - 一次系统地注释掉一行,直到找到 ReSharper 绊倒的那一行。然后您可以提取该方法并取消注释该行。

或者你可以手动重构它。

I have seen ReSharper do this same thing when the code I was trying to extract had a couple of throw statements.

You can do what I did in that case--systematically comment out one line at a time until you find the one that ReSharper trips over. Then you can extract the method and uncomment the line afterwards.

Or you can just refactor it by hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文