extension-methods

Evil use of Maybe monad and extension methods in C#?

穿精又带淫゛_ 提交于 2019-11-28 03:40:56
edit 2015 This question and its answers are no longer relevant. It was asked before the advent of C# 6, which has the null propagating opertor (?.), which obviates the hacky-workarounds discussed in this question and subsequent answers. As of 2015, in C# you should now use Form.ActiveForm?.ActiveControl?.Name. I've been thinking about the null propagation problem in .NET, which often leads to ugly, repeated code like this: Attempt #1 usual code: string activeControlName = null; var activeForm = Form.ActiveForm; if (activeForm != null) { var activeControl = activeForm.ActiveControl; if

Scala equivalent of C#’s extension methods?

与世无争的帅哥 提交于 2019-11-28 03:18:55
In C# you can write: using System.Numerics; namespace ExtensionTest { public static class MyExtensions { public static BigInteger Square(this BigInteger n) { return n * n; } static void Main(string[] args) { BigInteger two = new BigInteger(2); System.Console.WriteLine("The square of 2 is " + two.Square()); } }} How would this simple extension method look like in Scala? Mitch Blevins The Pimp My Library pattern is the analogous construction: object MyExtensions { implicit def richInt(i: Int) = new { def square = i * i } } object App extends Application { import MyExtensions._ val two = 2

Override (or shadow) a method with extension method? [duplicate]

[亡魂溺海] 提交于 2019-11-28 02:54:21
问题 Possible Duplicate: Is there any way in C# to override a class method with an extension method? Is it possible to override or shadow (new in C#) instance methods with extension methods? 回答1: No. From MSDN: You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in

How to add new methods to an existing type in Go?

天大地大妈咪最大 提交于 2019-11-28 02:48:48
I want to add a convenience util method on to gorilla/mux Route and Router types: package util import( "net/http" "github.com/0xor1/gorillaseed/src/server/lib/mux" ) func (r *mux.Route) Subroute(tpl string, h http.Handler) *mux.Route{ return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h) } func (r *mux.Router) Subroute(tpl string, h http.Handler) *mux.Route{ return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h) } but the compiler informs me Cannot define new methods on non-local type mux.Router So how would I achieve this? Do I create a new struct type that has

How can I know items is in the enum?

无人久伴 提交于 2019-11-28 02:09:52
问题 In this question, I use xor operator between enum with [Flags] attribute as following: [Flags] enum QueryFlag { None = 0x1, ByCustomer = 0x2, ByProduct = 0x4, ByDate = 0x8 } QueryFlag flags = QueryFlag.ByCustomer | QueryFlag.ByProduct; To add an QueryFlag, of course we should use | operator. flags |= QueryFlag.ByDate; To remove one, I have a different way with Dan Tao's answer. I'm using: flags ^= QueryFlag.ByProduct; while he is using: flags &= ~QueryFlag.ByProduct; Obviously his answer is

How to call a generic extension method with reflection?

此生再无相见时 提交于 2019-11-28 01:51:50
I wrote the extension method GenericExtension . Now I want to call the extension method Extension . But the value of methodInfo is always null. public static class MyClass { public static void GenericExtension<T>(this Form a, string b) where T : Form { // code... } public static void Extension(this Form a, string b, Type c) { MethodInfo methodInfo = typeof(Form).GetMethod("GenericExtension", new[] { typeof(string) }); MethodInfo methodInfoGeneric = methodInfo.MakeGenericMethod(new[] { c }); methodInfoGeneric.Invoke(a, new object[] { a, b }); } private static void Main(string[] args) { new Form

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

谁说胖子不能爱 提交于 2019-11-28 01:06:15
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 { internal static void Test1(this Program pg) { System.Console.WriteLine("OK"); } internal static void

Using Extension Methods with .NET Framework 2.0

懵懂的女人 提交于 2019-11-28 00:58:54
问题 Under Visual Studio 2008 Can I create an Extension Method to work under a .NET Framework 2.0 project? 回答1: There is an ugly hack that gets Extension methods working in .Net 2.0; but it would better just to upgrade your framework to 3.5. Alternate Sources: 1, 2. In short (from link #2): Extension methods are just normal static methods tagged with the [Extension] attribute. This attribute is actually just added by the compiler behind the scenes. In .NET 3.5, it lives in System.Core, so just

Extension Method in C# 2.0

こ雲淡風輕ζ 提交于 2019-11-28 00:53:41
What namespace do I need to get my extension to work Here is my Extension Method using System; using System.Collections.Generic; using System.Web; using System.Data; namespace MyUtilities { public static class DataReaderExtensions { public static DataTable ToDataTable(IDataReader reader) { DataTable table = new DataTable(); table.Load(reader); return table; } } } When I try to use it in like this Session["Master"] = cust.GetCustomerOrderSummary((string)Session["CustomerNumber"]).ToDataTable(); it doesn't work. This is .net 2.0 You can't. C# 2.0 doesn't have extension methods at all. You can

Extension methods conflict

谁说胖子不能爱 提交于 2019-11-28 00:37:58
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 { public Blah() { string a = "test"; int i = a.TestMethod(); //Which one is chosen ? } } } The Question