我正在重新调整我的解决方案,使用刚刚发布的Resharper版本(2016.2.2)
它标记这行代码:
ReportRunnerConstsAndUtils.ConvertValueToAppropriateTypeAndAssign(totalPackagesCell, packages);......通知我应该“ 调用扩展方法 ”
如果我默认,它会改变这一行:
totalPackagesCell.ConvertValueToAppropriateTypeAndAssign(packages);这是否更好? 如果是这样,怎么样? 为什么?
以下是正在调用的方法,它位于“ConstsAndUtils”类中:
// Adapted from https://stackoverflow.com/questions/26483496/is-it-possible-to-ignore-excel-warnings-when-generating-spreadsheets-using-epplu public static void ConvertValueToAppropriateTypeAndAssign(this ExcelRangeBase range, object value) { string strVal = value.ToString(); if (!String.IsNullOrEmpty(strVal)) { decimal decVal; double dVal; int iVal; if (decimal.TryParse(strVal, out decVal)) range.Value = decVal; if (double.TryParse(strVal, out dVal)) range.Value = dVal; else if (Int32.TryParse(strVal, out iVal)) range.Value = iVal; else range.Value = strVal; } else range.Value = null; }I am in the process of resharpening my solution, using the just-released version of Resharper (2016.2.2)
It flags this line of code:
ReportRunnerConstsAndUtils.ConvertValueToAppropriateTypeAndAssign(totalPackagesCell, packages);...intimating that I should "Invoke as extension method"
If I acquiesce, it changes that line to this:
totalPackagesCell.ConvertValueToAppropriateTypeAndAssign(packages);Is this better? If so, how? why?
Here is the method being called, which is in a "ConstsAndUtils" class:
// Adapted from https://stackoverflow.com/questions/26483496/is-it-possible-to-ignore-excel-warnings-when-generating-spreadsheets-using-epplu public static void ConvertValueToAppropriateTypeAndAssign(this ExcelRangeBase range, object value) { string strVal = value.ToString(); if (!String.IsNullOrEmpty(strVal)) { decimal decVal; double dVal; int iVal; if (decimal.TryParse(strVal, out decVal)) range.Value = decVal; if (double.TryParse(strVal, out dVal)) range.Value = dVal; else if (Int32.TryParse(strVal, out iVal)) range.Value = iVal; else range.Value = strVal; } else range.Value = null; }最满意答案
正如一些评论所指出的那样,这至少是部分偏好问题。 就我个人而言,我认为在这里使用扩展方法“更干净”和更清晰,但有些人可能会不同意这一点。
“引擎盖下”,当然,扩展方法是一个静态方法(不是实际的实例方法),只是编译器在这里给你一些语法糖(但除此之外)。
As some of the comments have indicated, this is at least partially a preference issue. Personally, I think it's "cleaner" and clearer to use an extension method here but some people may disagree with this.
"Under the hood," of course, the extension method is a static method (not an actual instance method), it's just that the compiler's giving you some syntactic sugar here (but that's besides the point).
更多推荐
ConvertValueToAppropriateTypeAndAssign,packages,totalPackagesCell,range,电脑培训,计算机
发布评论