extension-methods

Static extension methods supporting member constraints

99封情书 提交于 2019-12-17 16:59:36
问题 I need to implement a static extension method supporting member constraints on some basic primitive types like integers, floats, etc. Here's my code for signed integers: module MyOperators = let inline foo (x : ^T) = (^T : (static member Foo : ^T -> int) (x)) type System.Int32 with static member Foo(x : Int32) = 7 // static extension Test code: open MyOperators let x = foo 5 // x should be 7 But compiler complains with error: The type 'System.Int32' does not support any operators named 'Foo'

Why can't I call an extension method as a static method when using static import?

倖福魔咒の 提交于 2019-12-17 16:47:05
问题 Background: I had a static class, but the static methods weren't extension methods. I decided to refactor the methods into extension methods and didn't expect any code to break since extension methods can be called like static methods. However, code did break when static import was used for the static class holding the extension methods. Example: I have a static class with an extension method and a static method: namespace UsingStaticExtensionTest.Extensions { static class ExtensionClass {

Extension methods conflict

耗尽温柔 提交于 2019-12-17 16:42:00
问题 Lets say I have 2 extension methods to string, in 2 different namespaces: namespace test1 { public static class MyExtensions { public static int TestMethod(this String str) { return 1; } } } namespace test2 { public static class MyExtensions2 { public static int TestMethod(this String str) { return 2; } } } These methods are just for example, they don't really do anything. Now lets consider this piece of code: using System; using test1; using test2; namespace blah { public static class Blah {

Swift: Is it possible to add a protocol extension to a protocol?

一个人想着一个人 提交于 2019-12-17 16:28:19
问题 Lets say I have two protocols: protocol TheirPcol {} protocol MyPcol { func extraFunc() } What I want to do is to create a protocol extension for 'TheirPcol' which lets extraFunc() work on anything which conforms to 'TheirPcol'. So something like this: extension TheirPcol : MyPcol { // Error 'Extension of protocol 'TheirPcol' cannot have an inheritance clause. func extraFunc() { /* do magic */} } struct TheirStruct:TheirPcol {} let inst = TheirStruct() inst.extraFunc() The kicker in this is

Calculating Count for IEnumerable (Non Generic)

我的未来我决定 提交于 2019-12-17 15:47:24
问题 Can anyone help me with a Count extension method for IEnumerable (non generic interface). I know it is not supported in LINQ but how to write it manually? 回答1: The simplest form would be: public static int Count(this IEnumerable source) { int c = 0; using (var e = source.GetEnumerator()) { while (e.MoveNext()) c++; } return c; } You can then improve on this by querying for ICollection : public static int Count(this IEnumerable source) { var col = source as ICollection; if (col != null) return

How to call extension method which has the same name as an existing method? [duplicate]

不羁岁月 提交于 2019-12-17 13:54:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is there any way in C# to override a class method with an extension method? I have code like public class TestA { public string ColA { get; set; } public string ColB { get; set; } public string ColC { get; set; } public void MethodA() { MessageBox.Show("Original A1."); } } static class ExtenstionTest { public static void MethodA(this TestA A1) { MessageBox.Show("Extended A1."); } } Now if I call MethodA like

How to call extension method which has the same name as an existing method? [duplicate]

故事扮演 提交于 2019-12-17 13:53:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is there any way in C# to override a class method with an extension method? I have code like public class TestA { public string ColA { get; set; } public string ColB { get; set; } public string ColC { get; set; } public void MethodA() { MessageBox.Show("Original A1."); } } static class ExtenstionTest { public static void MethodA(this TestA A1) { MessageBox.Show("Extended A1."); } } Now if I call MethodA like

VB.NET: impossible to use Extension method on System.Object instance

人盡茶涼 提交于 2019-12-17 11:17:34
问题 Can I make an Extension method for all the subclasses of System.Object (everything)? Example: <Extension> Public Function MyExtension(value As Object) As Object Return value End Function The above functions won't work for object instance: Dim myObj1 As New Object() Dim myObj2 = myObj1.MyExtension() The compiler does not accept it, is the problem in my computer? :) UPDATE The problem seems to occur only in VB, where members of object are looked-up by reflection (late-bound). UPDATE AFTER

Ambiguous call between two C# extension generic methods one where T:class and other where T:struct

跟風遠走 提交于 2019-12-17 10:18:52
问题 Consider two extension methods: public static T MyExtension<T>(this T o) where T:class public static T MyExtension<T>(this T o) where T:struct And a class: class MyClass() { ... } Now call the extension method on a instance of the above class: var o = new MyClass(...); o.MyExtension(); //compiler error here.. o.MyExtension<MyClass>(); //tried this as well - still compiler error.. The compiler says that calling the method is an ambiguous call when I call it on a class. I would have thought

How do I use Active Support core extensions?

有些话、适合烂在心里 提交于 2019-12-17 08:17:44
问题 I have Active Support 3.0.3 installed and Rails 3.0.3 with Ruby 1.8.7. When I try to use 1.week.ago I get NoMethodError: undefined method 'week' for 1:Fixnum from (irb):2 The other core extensions seem to work. I tried it on a friend's computer (same install specs and legacy versions are on his) with the same results. What gives? All of this is in IRB. 回答1: Since using Rails should handle this automatically I'm going to assume you're trying to add Active Support to a non-Rails script. Read