extension-methods

C# Extension Methods - How far is too far?

会有一股神秘感。 提交于 2019-12-03 06:51:04
问题 Rails introduced some core extensions to Ruby like 3.days.from_now which returns, as you'd expect a date three days in the future. With extension methods in C# we can now do something similar: static class Extensions { public static TimeSpan Days(this int i) { return new TimeSpan(i, 0, 0, 0, 0); } public static DateTime FromNow(this TimeSpan ts) { return DateTime.Now.Add(ts); } } class Program { static void Main(string[] args) { Console.WriteLine( 3.Days().FromNow() ); } } Or how about:

Visual Studio's “auto-resolve” feature doesn't work for extension methods - what now?

女生的网名这么多〃 提交于 2019-12-03 06:38:08
I love the "Resolve" feature in visual studio. Typical scenario: Type in Debug Type . Notice that no intellisense appears Right-click Select Resolve Choose using System.Diagnostics or System.Diagnostics.Debug Beautiful. Use it all the time. Extension method scenario: Type in var maxNumber = new int[] {1, 2, 3, 4} Type . Notice that intellisense brings up array methods but no LINQ extension methods Manually type Max() Right-click Max() No Resolve to be found Right click on int[] Still no Resolve to be found Begrudgingly scroll to the top of the page and enter using System.Linq; * *assuming you

Best practices: C# Extension methods namespace and promoting extension methods

匆匆过客 提交于 2019-12-03 06:30:16
问题 I know there exists already a post, describing nearly the same, but I think mine is a bit different. What I would like to know is how you organize your extension methods in terms of assigning the namespace. Currently - for the extension methods in our framework - I use the following namespace pattern MyCompany.Web.Utils and inside I have the extension method classes. This is fine for me with the disadvantage that the extenders are not immediately visible to our software developers. Consider

Linq To SQL problem - has no supported translation to SQL (problem with C# property)

旧城冷巷雨未停 提交于 2019-12-03 05:26:52
I'm extending some Linq to SQL classes. I've got 2 similar statements, the 1st one works, the 2nd does not ("has no supported translation to SQL" error). var reg2 = rs.ProductRegistrations().SingleOrDefault(p => p.Product.product_name == "ACE") var reg5 = rs.ProductRegistrations().SingleOrDefault(p => p.product_name == "ACE"); After reading this link LINQ: No Translation to SQL I understand (I think), that basically everything needs to be "inline", otherwise the expression tree can not be calculated correctly. The 1st example directly accesses the LinqToSql EntitySet "Product" (keeping

Can C# extension methods access private variables?

风流意气都作罢 提交于 2019-12-03 05:21:53
问题 Is it possible to access an object's private variables using an extension method? 回答1: No. You can do the same in an extension method as in a "normal" static method in some utility class. So this extension method public static void SomeMethod(this string s) { // do something with 's' } is equivalent to some static helper method like this (at least regarding what you can access): public static void SomeStringMethod(string s) { // do something with 's' } (Of course you could use some reflection

What is the motivation behind “Use Extension Methods Sparingly?”

爷,独闯天下 提交于 2019-12-03 03:32:56
I find them a very natural way to extend existing classes, especially when you just need to "spot-weld" some functionality onto an existing class. Microsoft says, "In general, we recommend that you implement extension methods sparingly and only when you have to." And yet extension methods form the foundation of Linq; in fact, Linq was the reason extension methods were created. Are there specific design criteria where using extension methods are perferred over inheritance or composition? Under what criteria are they discouraged? Eric Lippert We show proposed new language features (at least the

“unrecognized selector sent to class” when calling category method from a library

自古美人都是妖i 提交于 2019-12-03 03:06:48
Problem This question may seem a bit long, but I try to give as much information as possible, since I am really staggered by this. I am currently working an a library which should automate XML document parsing. But I am running into a problem now testing the library for the first time. I have a library class called CSXDocumentLayout which represents the layout of a document. This class contains the private method - (NSError *)readLayoutDocument:(NSString *)fpath called from an init method. /* MARK: Reading in Layouts */ - (NSError *)readLayoutDocument:(NSString *)fpath { CSXDocumentLayout

Adding an extension method to the string class - C#

江枫思渺然 提交于 2019-12-03 02:10:34
Not sure what I'm doing wrong here. The extension method is not recognized. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using StringExtensions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { RunTests(); } static void RunTests() { try { ///SafeFormat SafeFormat("Hi There"); SafeFormat("test {0}", "value"); SafeFormat("test missing second value {0} - {1}", "test1"); SafeFormat("{0}"); //regular format RegularFormat("Hi There"); RegularFormat("test {0}", "value"); RegularFormat("test

Interface + Extension (mixin) vs Base Class

杀马特。学长 韩版系。学妹 提交于 2019-12-03 01:58:16
问题 Is an interface + extension methods (mixin) preferable to an abstract class? If your answer is "it depends", what does it depend upon? I see two possible advantages to the interface + extension approach. Interfaces are multiply inheritable and classes are not. You can use extension methods to extend interfaces in a non-breaking way. (Clients that implement your interface will gain your new base implementation but still be able to override it.) I have not yet thought of a downside to this

Decouple EF queries from BL - Extension Methods VS Class-Per-Query

江枫思渺然 提交于 2019-12-03 00:29:53
I have read dozens of posts about PROs and CONs of trying to mock \ fake EF in the business logic. I have not yet decided what to do - but one thing I know is - I have to separate the queries from the business logic. In this post I saw that Ladislav has answered that there are 2 good ways: Let them be where they are and use custom extension methods, query views, mapped database views or custom defining queries to define reusable parts. Expose every single query as method on some separate class. The method mustn't expose IQueryable and mustn't accept Expression as parameter = whole query logic