extension-methods

resharper intellisense problem with extension methods

一曲冷凌霜 提交于 2019-12-05 03:21:47
so, I have a repository defined with a method like this: IQueryable<Customer> Customers{...} and elsewhere an extension method to filter the customers like so: public static IQueryable<Customer> WithID(this IQueryable<Customer> customers, int ID){...} and this woks nicely, letting me use the repository like this: var c = repo.Customers().WithID(5).Single(); but the problem is, ReSharper messes up the Auto-Completion on this big time. When I type var c = repo.Customers().Wi I get nice Intellisense showing me the WithID(...) method, but when I cursor down to it and hit TAB, instead of getting

Command failed due to signal: Segmentation fault: 11 while emitting IR SIL function

只谈情不闲聊 提交于 2019-12-05 03:21:42
I want to add closure properties in the extension of UITextView so I define a closure using typealias: typealias TextViewHeightDidChangedClosure = (_ currentTextViewHeight:CGFloat)->Void extension UITextView{ func setTextViewHeightDidChanged(textViewHeightDidChanged:TextViewHeightDidChangedBlock){ objc_setAssociatedObject(self, &TextViewHeightDidChangedBlockKey, textViewHeightDidChanged, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC) } func textViewHeightDidChanged()->TextViewHeightDidChangedBlock?{ let textChanged : ((CGFloat)->Void) = objc_getAssociatedObject(self,

What overhead is associated with an extension method at runtime? (.NET) [duplicate]

我们两清 提交于 2019-12-05 03:11:52
Possible Duplicate: Extension Method Performance In a data crunching application that is CPU and/or memory access bound, is the overhead of a one line extension method noticable? Is it any higher than a normal function call, or is it simply a compiler/IDE abstraction? For instance, would the following function be ill advised if it was being called upwards of several thousand times a second: public static void WriteElementString(this XmlTextWriter writer, string name, int data) { writer.WriteElementString(name, data.ToString()); } There's no overhead. It's just a static method called with

Extension Method to Get the Values of Any Enum

十年热恋 提交于 2019-12-05 01:33:19
I've been trying to create an extension method, that would work on any enum, to return its values. Instead of doing this: Enum.GetValues(typeof(BiasCode)).Cast<BiasCode>() It would be nice to do this: new BiasCode().Values() It would even be better without new , but that's another issue. I have a .NET fiddle that has a solution that's close (code shown below). The problem with this code is that the extension method is returning List<int> . I would like to have it return a list of the enum values itself. Returning List<int> isn't terrible; it just means I have to cast the result. Is it even

Null safe way to get values from an IDataReader

你。 提交于 2019-12-05 01:32:17
(LocalVariable)ABC.string(Name)= (Idatareader)datareader.GetString(0); this name value is coming from database.. what happening here is if this name value is null while reading it's throwing an exception? I am manually doing some if condition here. I don't want to write a manual condition to check all my variables.. I am doing something like this now.. string abc = (Idatareader)datareader.GetValue(0); if(abc = null) //assiging null else assiging abc value is there something like can we write extension method for this? thanks Here is a couple extension methods that will nicely wrap up all of

Extension methods for specific generic types

a 夏天 提交于 2019-12-05 01:28:08
I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me: What I want to do is something like the following: type IEnumerable<int> with member this.foo = this.ToString() Yet it gives me the compiler error (underlining the int keyword): Unexpected identifier in type name. Expected infix operator, quote symbol or other token. The following does work, though it does not specifically bind the generic type parameter to int , as I want: type IEnumerable<'a> with member this.foo = this

Is it possible to create Extension Methods with 2.0 Framework?

♀尐吖头ヾ 提交于 2019-12-05 01:17:04
I was wondering if there is a way to create extension methods using Visual Studio 2005 and the 2.0 framework? public static class StringExtensions { public static void SomeExtension(this String targetString) { } } If there is no way to do this, what would the equivalent be? Just create static methods in some sort of library class? You can create extension methods using .Net framework 2.0, if you use the C# 3.0 compiler and Visual Studio 2008 or greater. The catch is that you have to add this code to your project: namespace System.Runtime.CompilerServices { public class ExtensionAttribute :

ICollection / ICollection<T> ambiguity problem

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 00:50:37
问题 Just want to make simple extension for syntactic sygar : public static bool IsNotEmpty(this ICollection obj) { return ((obj != null) && (obj.Count > 0)); } public static bool IsNotEmpty<T>(this ICollection<T> obj) { return ((obj != null) && (obj.Count > 0)); } It works perfectly when I work with some collections, but when working with others I get The call is ambiguous between the following methods or properties: 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)' and 'PowerOn

Why don't my HtmlHelper extensions work?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 00:42:24
I'm building an ASP.Net MVC website. Rather than have everything in one project, I've decided to separate the Web, Model and Controller out into different projects in the same solution, that reference each-other. The referencing goes like this: Web ---[references]---> Controller ---[references]---> Model Now I wanted to add 2 custom methods to the HtmlHelper class - they're called "IncludeScript" and "IncludeStyle". They each take a single string parameter, and generate a script or link tag respectively. I've created an extender class, according to documentation on the web, and written the two

When do Extension Methods break?

拈花ヽ惹草 提交于 2019-12-05 00:20:34
We are currently discussing whether Extension methods in .NET are bad or not. Or under what circumstances Extension methods can introduce hard to find bugs or in any other way behave unexpectedly. We came up with: Writing an extension method for types that are not under your control (e.g. extending DirectoryInfo with GetTotalSize(), etc...) is bad, because the owner of the API could introduce a method that hides our extension - and might have different edge cases. For example testing for null in an extension method will automatically translate into a NullReferenceException if the extension