问题标签 [extension-methods]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - 为什么我不能使用扩展方法隐式转换委托?
我正在尝试找出一种自动将某些内容投射到 Action 或 Func 的方法,而我能想到的最好的方法是这样的:
必须有更好/更简单的方法来做到这一点,有什么想法吗?
c# - Using Extensions: Weighing The Pros vs Cons
Recently I asked a question about how to clean up what I considered ugly code. One recommendation was to create an Extension Method that would perform the desired function and return back what I wanted. My first thought was 'Great! How cool are Extensions...' but after a little more thinking I am starting to have second thoughts about using Extensions...
My main concern is that it seems like Extensions are a custom 'shortcut' that can make it hard for other developers to follow. I understand using an Extension can help make the code syntax easier to read, but what about following the voice behind the curtain?
Take for example my previous questions code snippet:
Now replace it with an Extension:
and call using the syntax:
Definetely a handy way to go, but is it really worth the overhead of another class object? And what happens if someone wants to reuse my code snippet but doesn't understand Extension? I could have just as easily used the syntax with the same result:
So I did a little digging and found a couple of articles that talked about the pros/cons of using Extensions. For those inclined have a look at the following links:
Extension Methods Best Practice
I can't really decide which is the better way to go. Custom Extensions that do what I want them to do or more displayed code to accomplish the same task? I would be really interested in learning what 'real' developers think about this topic...
c# - C#, Linq2SQL: Creating a predicate to find elements within a number of ranges
Lets say I have something called Stuff in my database, with a property called Id. From the user I get a sequence of selected Range objects (or rather I create them from their input) with the Ids they want. A stripped down version of that struct looks like this:
So one could for example have gotten:
I then want to use that to create a predicate to select only things which have a value between those. For example, using the PredicateBuilder, I can for example do that this way:
and then:
Which works! What I would like to do now, is to create a generic extension method to create those predicates for me. Kind of like this:
Problem here, is that it crashes with a NotSupportedException because of the selector(ø) call: Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL.
I guess that is understandable. But is there any way to get around this? What I would like to end up with is so that I could just do:
Or even better, create something that returns an IQueryable so that I could just do:
So, any ideas? I would really like to get this working, cause if not I will get A LOT of those foreach blocks of code, creating predicates...
Note 1: Of course, would be nice if I could expand to more than int, like DateTime and such, but not sure how that ends up with using the >= and <= operators... Does CompareTo work with linq-to-sql? If not there is no problem creating two. One for int and one for DateTime, since that is mostly the types this will be used for.
Note 2: It is going to be used for reporting, where the user is going to be able to narrow down what comes out, based on different things. Like, I want this report for those people and those dates.
c# - 如何使用 Moq 模拟扩展方法?
我正在编写一个依赖于扩展方法结果的测试,但我不希望该扩展方法未来的失败会破坏这个测试。模拟该结果似乎是显而易见的选择,但Moq 似乎没有提供覆盖静态方法的方法(扩展方法的要求)。Moq.Protected 和 Moq.Stub 也有类似的想法,但它们似乎没有为这种情况提供任何东西。我错过了什么还是应该以不同的方式解决这个问题?
这是一个简单的示例,该示例因通常的“对不可覆盖成员的无效期望”而失败。这是一个需要模拟扩展方法的坏例子,但它应该这样做。
至于任何可能建议我使用 Isolator 的 TypeMock 爱好者:我很欣赏你的努力,因为看起来 TypeMock 可以蒙着眼睛和醉酒地完成这项工作,但我们的预算不会很快增加。
c# - 有没有办法可以从包含代码的字符串中动态定义谓词主体?
这可能是一个愚蠢的问题,但在这里。就此而言,我希望能够从从数据库 VARCHAR 列或任何字符串解析的字符串动态构造谓词 < T >。例如,假设数据库中的列包含以下字符串:
这些代码/字符串值将存储在数据库中,知道通用“e”的可能属性是什么,并且知道它们必须返回一个布尔值。然后,在一个神奇、奇妙、奇幻的世界中,代码可以在不知道谓词是什么的情况下执行,例如:
或 Lambda 化:
我知道它可能永远不会这么简单,但是有没有办法,也许使用 Reflection.Emit,从文本动态创建委托代码并将其提供给 FindAll < T > (或任何其他匿名/扩展)方法?
c# - 使用 LINQ 替换循环是否明智?
现在我们有了 LINQ 的强大功能,我想知道哪种语法更可取。例如,我找到了以下方法(只是认为这是一个很好的例子):
如果我们将其转换为 LINQ 方法,它看起来像这样(未经测试):
您更愿意看到和维护哪个?这是疯子还是天才?
sql - 为什么 LINQ to SQL 会被扩展方法欺骗?现在怎么办?
考虑我的Event
班级,并且我将DateTime
' 存储在数据库中作为 UTC 日期。我只是想根据特定时区的当前日期返回过滤范围 - 很简单吧?
这工作正常:
这也可以正常工作:
.. 并且还满足我的时区要求。
所以在这里我想我可以把这个特定的转换移到这个扩展方法中:
这不起作用:
.. 我得到这个 NotSupportedException: Method 'System.DateTime RiyadhTimeFromUtc(System.DateTime)' has no supported translation to SQL.
这显然是垃圾,因为当扩展方法中没有相同的代码时,编译器很乐意将其转换为 SQL。
在某些类型和最近的 DateTime 之前,我遇到过“没有支持的 SQL 转换”问题。但是我上面的测试和这个链接证明在 SQL 翻译中应该支持 AddHours 方法。
如果有人能告诉我我在这里做错了什么(或解决此问题的不同解决方法),我将不胜感激。
c# - 扩展方法何时中断?
我们目前正在讨论 .NET 中的扩展方法是否不好。或者在什么情况下扩展方法会引入难以发现的错误或以任何其他方式出现意外行为。
我们想出了:
- 为不受您控制的类型编写扩展方法(例如,使用 GetTotalSize() 扩展 DirectoryInfo 等)是不好的,因为 API 的所有者可能会引入隐藏我们扩展的方法 - 并且可能有不同的边缘情况. 例如,如果由于隐藏而不再使用扩展方法,则在扩展方法中测试 null 将自动转换为 NullReferenceException。
问题:
- 除了“隐藏”之外,还有其他我们没有想到的危险情况吗?
编辑:
另一个非常危险的情况。假设你有一个扩展方法:
并使用它:
请注意,您使用它的名称空间更长。
现在你用这个引用一个dll:
你的测试将失败!它将在没有编译器错误的情况下编译。它只会失败。您甚至不必指定“使用 Example.ExtensionMethods.Conflict”。编译器将遍历命名空间名称并在 Example.ExtensionMethods.Extension 之前找到 Example.ExtensionMethods.Conflict.ConflictExtension 并将使用它而不会抱怨不明确的扩展方法。哦,恐怖!
c# - C# 有扩展属性吗?
C# 有扩展属性吗?
例如,我可以将扩展属性添加到DateTimeFormatInfo
调用ShortDateLongTimeFormat
which 会返回ShortDatePattern + " " + LongTimePattern
吗?
c# - 选择 IEnumerable 的下 N 个元素
假设您有一些称为 S 的长度为 N 的 IEnumerable。我想从 S 中选择所有长度为 n <= N 的连续子序列。
如果 S 是,比如说,一个字符串,这会很容易。有 (S.Length - n + 1) 个长度为 n 的子序列。例如,“abcdefg”的长度为 (7),这意味着它有 (5) 个长度为 (3) 的子字符串:“abc”、“bcd”、“cde”、“def”、“efg”。
但是 S 可以是任何 IEnumerable,所以这条路线是不开放的。如何使用扩展方法来解决这个问题?