extension-methods

Implementing a function with a default parameter defined in a protocol

♀尐吖头ヾ 提交于 2019-11-28 12:10:35
Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the default implementation is only used as a "fallback" : It's executed when a type conforms to the protocol but doesn't provide its own implementation. At least that's how I read The Swift Programming Language guide: If a conforming type provides its own implementation of a required method or property, that implementation will be used instead of the one provided by the extension. Now I ran into a situation where my

Usage of Extension Methods

可紊 提交于 2019-11-28 12:01:54
When does using extension methods make sense? Does adding extension methods to a type affect performance? These questions are follow up to the question i asked earlier about Extension Methods . When does using extension methods make sense? They make sense when you are using LINQ and want to chain or pipe functional output from one function to another. It improves the readability of the code and allows you to express a concept more elegantly (whatever that is worth). They also allow you to give the appearance of instance methods on any type you like without modifying the source of that type,

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

◇◆丶佛笑我妖孽 提交于 2019-11-28 11:54:21
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 TestA a = new TestA(); a.MethodA(); It will always call Original method. How can I call the extension method. You can't call the extension method as a normal extension

Escape Catch-22 with extension attributes in .NET 2.0

怎甘沉沦 提交于 2019-11-28 11:11:55
How can a single .NET assembly, targeting 2.0, 3.0, 3.5, 4.0, and 4.5 concurrently, support extension methods for both C# and VB.NET consumers? The standard suggestion is to add this: namespace System.Runtime.CompilerServices { public sealed class ExtensionAttribute : Attribute { } } This the approach suggested by more than one Microsoft employee and was even featured in MSDN magazine . It's widely hailed by many bloggers as having 'no ill effects'. Oh, except it will cause a compiler error from a VB.NET project targeting .NET 3.5 or higher. The authors of Microsoft.Core.Scripting.dll figured

Workarounds for using custom methods/extension methods in LINQ to Entities

孤人 提交于 2019-11-28 10:09:44
I have defined a GenericRepository class which does the db interaction. protected GenericRepository rep = new GenericRepository(); And in my BLL classes, I can query the db like: public List<Album> GetVisibleAlbums(int accessLevel) { return rep.Find<Album>(a => a.AccessLevel.BinaryAnd(accessLevel)).ToList(); } BinaryAnd is an extension method which checks two int values bit by bit. e.g. AccessLevel=5 => AccessLevel.BinaryAnd(5) and AccessLevel.binaryAnd(1) both return true. However I cannot use this extension method in my LINQ queries. I get a runtime error as follows: LINQ to Entities does

How to define a type extension for T[] in F#?

拈花ヽ惹草 提交于 2019-11-28 09:50:35
In C#, I can define an extension method for a generic array of type T like this: public static T GetOrDefault<T>(this T[] arr, int n) { if (arr.Length > n) { return arr[n]; } return default(T); } but for the life of me I can't figure out how to do the same in F#! I tried type 'a array with , type array<'a> with and type 'a[] with and the compiler wasn't happy with any of them. Can anyone tell me what's the right to do this in F#? Sure, I can do this by overshadowing the Array module and add a function for that easily enough, but I really want to know how to do it as an extension method! You

How to access extension of UIColor in SWIFT?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 09:44:47
i am very new to swift and trying to create an extension of UIColor class as extension UIColor{ func getCustomBlueColor() -> UIColor{ return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00) } After this i accessed the method as btnShare.setTitleColor(UIColor.getCustomBlueColor(**UIColor**), forState: UIControlState.Normal) I dont know what I should pass as an argument to this statement. You have defined an instance method , which means that you can call it only on an UIColor instance: let col = UIColor().getCustomBlueColor() // or in your case: btnShare.setTitleColor(UIColor()

Extension Methods not Recognized

こ雲淡風輕ζ 提交于 2019-11-28 09:35:17
What is necessary to have an extension method honored when it exists in an imported assembly? I built one in a class library project but it is not recognized in my web project which references the library. All the other classes and methods in the library are honored and visible but this extension method is not. The extension method is visible when used within the library. Referencing an assembly containing a class with extension methods is not enough. You need to import the namespace containing the class in each of your source file where you want to use the extension methods. For example, to

C# Count() Extension Method Performance

拥有回忆 提交于 2019-11-28 09:31:50
问题 If the LINQ Count() extension method is invoked on an IEnumerable<T> that has a Count property (e.g. List<T> ), does the Count() method look for that property and return it (rather than counting the items by enumerating them)? The following test code seems to indicate that it does: using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace CountSpeedTest { // Output: // List - CLR : 0 ms // Enumerate - CLR : 10 ms // List -

Extending a solution for simple binding to a 'Text property to multiple Controls to handle binding to any Type?

别等时光非礼了梦想. 提交于 2019-11-28 09:12:20
问题 My question is : how to move beyond writing a custom implementation of a technique for databinding multiple controls (controls without built-in DataSource properties), for each possible type of data , to simple properties ... as described and demonstrated in code that follows ... to achieve a more poweful solution that will be independent of whether the binding is to a string, or an int, or other types. My guess is: this will involve reflection; but, I'm stuck at that point. I'm looking for