extension-methods

Reflection to Identify Extension Methods

徘徊边缘 提交于 2019-11-26 00:58:26
问题 In C# is there a technique using reflection to determine if a method has been added to a class as an extension method? Given an extension method such as the one shown below is it possible to determine that Reverse() has been added to the string class? public static class StringExtensions { public static string Reverse(this string value) { char[] cArray = value.ToCharArray(); Array.Reverse(cArray); return new string(cArray); } } We\'re looking for a mechanism to determine in unit testing that

Does C# have extension properties?

冷暖自知 提交于 2019-11-26 00:57:39
问题 Does C# have extension properties? For example, can I add an extension property to DateTimeFormatInfo called ShortDateLongTimeFormat which would return ShortDatePattern + \" \" + LongTimePattern ? 回答1: No they do not exist in C# 3.0 and will not be added in 4.0. It's on the list of feature wants for C# so it may be added at a future date. At this point the best you can do is GetXXX style extension methods. 回答2: No, they don't exist. I know that the C# team was considering them at one point

What are Extension Methods?

孤人 提交于 2019-11-26 00:34:10
问题 What are extension methods in .NET? EDIT: I have posted a follow up question at Usage of Extension Methods 回答1: Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. Extension Methods help blend the flexibility of "duck typing" support popular within dynamic languages today with the performance and compile-time validation of strongly-typed languages. Reference: http://weblogs.asp.net

Can I add extension methods to an existing static class?

老子叫甜甜 提交于 2019-11-26 00:09:31
问题 I\'m a fan of extension methods in C#, but haven\'t had any success adding an extension method to a static class, such as Console. For example, if I want to add an extension to Console, called \'WriteBlueLine\', so that I can go: Console.WriteBlueLine(\"This text is blue\"); I tried this by adding a local, public static method, with Console as a \'this\' parameter... but no dice! public static class Helpers { public static void WriteBlueLine(this Console c, string text) { Console

Distinct() with lambda?

◇◆丶佛笑我妖孽 提交于 2019-11-26 00:06:41
问题 Right, so I have an enumerable and wish to get distinct values from it. Using System.Linq , there\'s of course an extension method called Distinct . In the simple case, it can be used with no parameters, like: var distinctValues = myStringList.Distinct(); Well and good, but if I have an enumerable of objects for which I need to specify equality, the only available overload is: var distinctValues = myCustomerList.Distinct(someEqualityComparer); The equality comparer argument must be an

Why there is no ForEach extension method on IEnumerable?

半世苍凉 提交于 2019-11-25 23:37:16
问题 Inspired by another question asking about the missing Zip function: Why is there no ForEach extension method in the Enumerable class? Or anywhere? The only class that gets a ForEach method is List<> . Is there a reason why it\'s missing (performance)? 回答1: There is already a foreach statement included in the language that does the job most of the time. I'd hate to see the following: list.ForEach( item => { item.DoSomething(); } ); Instead of: foreach(Item item in list) { item.DoSomething(); }

Which method performs better: .Any() vs .Count() > 0?

痴心易碎 提交于 2019-11-25 23:05:40
问题 in the System.Linq namespace, we can now extend our IEnumerable \'s to have the Any() and Count() extension methods . I was told recently that if i want to check that a collection contains 1 or more items inside it, I should use the .Any() extension method instead of the .Count() > 0 extension method because the .Count() extension method has to iterate through all the items. Secondly, some collections have a property (not an extension method) that is Count or Length . Would it be better to

Mocking Extension Methods with Moq

馋奶兔 提交于 2019-11-25 21:52:36
I have a preexisting Interface... public interface ISomeInterface { void SomeMethod(); } and I've extended this intreface using a mixin... public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface someInterface) { // Implementation here } } I have a class thats calling this which I want to test... public class Caller { private readonly ISomeInterface someInterface; public Caller(ISomeInterface someInterface) { this.someInterface = someInterface; } public void Main() { someInterface.AnotherMethod(); } } and a test where I'd like to mock the interface and

Reflection to Identify Extension Methods

无人久伴 提交于 2019-11-25 20:56:48
In C# is there a technique using reflection to determine if a method has been added to a class as an extension method? Given an extension method such as the one shown below is it possible to determine that Reverse() has been added to the string class? public static class StringExtensions { public static string Reverse(this string value) { char[] cArray = value.ToCharArray(); Array.Reverse(cArray); return new string(cArray); } } We're looking for a mechanism to determine in unit testing that the extension method was appropriately added by the developer. One reason to attempt this is that it is

Does C# have extension properties?

…衆ロ難τιáo~ 提交于 2019-11-25 20:33:10
Does C# have extension properties? For example, can I add an extension property to DateTimeFormatInfo called ShortDateLongTimeFormat which would return ShortDatePattern + " " + LongTimePattern ? No they do not exist in C# 3.0 and will not be added in 4.0. It's on the list of feature wants for C# so it may be added at a future date. At this point the best you can do is GetXXX style extension methods. Jon Skeet No, they don't exist. I know that the C# team was considering them at one point (or at least Eric Lippert was) - along with extension constructors and operators (those may take a while to