为什么我会收到 ReSharper 错误“提取的代码有多个入口点”?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能遇到了已知问题:
选择 foreach 循环和 extract 方法。它给出了奇怪的警告
该片段有多个入口点(??!)并导致
以下代码:
最好用简单的
return 替换生成的 foreach
。Foo(源文件,目标路径);
It looks like you may have encountered a known issue:
Select both foreach-loops and extract method. It gives strange warning
that the fragment has multiple entry points (??!) and results in the
following code:
It would be better to replace generated foreach with simple
return
.Foo(sourceFile, targetPath);
当我试图提取的代码有几个 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.