(为什么)正在调用作为扩展方法的首选方式?((Why) is invoking as extension method the preferred…way?)

编程入门 行业动态 更新时间:2024-10-28 04:20:23
(为什么)正在调用作为扩展方法的首选方式?((Why) is invoking as extension method the preferred…way?)

我正在重新调整我的解决方案,使用刚刚发布的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,电脑培训,计算机

本文发布于:2023-08-04 11:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:首选   方式   方法   invoking   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!