extension-methods

Is it possible to add behavior to a non-dynamic ActionScript 3 class without inheriting the class?

What I'd like to do is something like the following: FooClass.prototype.method = function():String { return "Something"; } var foo:FooClass = new FooClass(); foo.method(); Which is to say, I'd like to extend a generated class with a single method, not via inheritance but via the prototype. The class is generated from a WSDL, it'...

Extension interface patterns

The new extensions in .Net 3.5 allow functionality to be split out from interfaces. For instance in .Net 2.0 public interface IHaveChildren { string ParentType { get; } int ParentId { get; } List<IChild> GetChildren() } Can (in 3.5) become: public interface IHaveChildren { string ParentType { get; } int ParentId...

Generic List Extensions in C#

I am writing a few extensions to mimic the map and reduce functions in Lisp. public delegate R ReduceFunction<T,R>(T t, R previous); public delegate void TransformFunction<T>(T t, params object[] args); public static R Reduce<T,R>(this List<T> list, ReduceFunction<T,R> r, R initial) { var aggregate = initial; foreach(var t in...

How do you write a C# Extension Method for a Generically Typed Class

Hi, This should hopefully be a simple one. I would like to add an extension method to the System.Web.Mvc.ViewPage< T > class. How should this extension method look? My first intuitive thought is something like this: namespace System.Web.Mvc { public static class ViewPageExtensions { public static string GetDefaultPag...

Extension Methods not working for an interface

Inspired by the MVC storefront the latest project I'm working on is using extension methods on IQueryable to filter results. I have this interface; IPrimaryKey { int ID { get; } } and I have this extension method public static IPrimaryKey GetByID(this IQueryable<IPrimaryKey> source, int id) { return source(obj => obj.ID == id)...

Organizing Extension Methods

How do you organize your Extension Methods? Say if I had extensions for the object class and string class I'm tempted to separate these extension methods into classes IE: public class ObjectExtensions { ... } public class StringExtensions { ... } am I making this too complicated or does this make sense? ...

.NET List<T> Concat vs AddRange

What is the difference between the AddRange and Concat functions on a generic List? Is one recommended over the other? ...

Why is there not a ForEach extension method on the IEnumerable interface?

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)? ...

Possible pitfalls of using this (extension method based) shorthand

I regularly want to access properties on possibly null objects: string propertyValue1 = null; if( myObject1 != null ) propertyValue1 = myObject1.StringProperty; int propertyValue2 = 0; if( myObject2 != null ) propertyValue2 = myObject2.IntProperty; And so on... I use this so often that I have a snippet for it. You can short...

Is there any way to use an extension method in an object initializer block in C#

The simple demo below captures what I am trying to do. In the real program, I have to use the object initialiser block since it is reading a list in a LINQ to SQl select expression, and there is a value that that I want to read off the database and store on the object, but the object doesn't have a simple property that I can set for that...

Define a .NET extension method with solution scope

I have a few 'helper' style extension methods I use quite regularly now (they are mostly quite simple, intuitive, and work for good not evil, so please don't have this descend into a discussion around whether or not I should use them). They are largely extending core .NET CLR classes. Currently, I have to copy the 'ExtensionMethods.cs' ...

Operator Overloading with C# Extension Methods

I'm attempting to use extension methods to add an operater overload to the C# StringBuilder class. Specifically, given StringBuilder sb, I'd like sb += "text" to become equivalent to sb.Append("text"); Here's the syntax for creating an extension method for StringBuilder: public static class sbExtensions { public static StringBuild...

Error when using extension methods in C#

I came across an issue that makes me think there is bug in the 3.0 framework. When I try to use extension methods I get the following error: Missing compiler required member 'System.Runtime.CompilerServices.ExtensionAttribute..ctor' When using this simple code: public static class StringUtils { static void TestExtension(...

How can I call C# extension methods in VB code

I have a class library with some extension methods written in C# and an old website written in VB. I want to call my extension methods from the VB code but they don't appear in intelisense and I get compile errors when I visit the site. I have got all the required Imports because other classes contained in the same namespaces are appea...

Extension method output in Container.DataItem?

So I have an IList of business entities that I loop through in a ListView into an unordered list. I created an extension method on this Entity in my presentation layer. In code behind, I can Response.Write the result of this extension method, but when I try to access it through the ListView I get an error. The method is called IsCurrent ...

Why don't my HtmlHelper extensions work?

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 eachother. The referencing goes like this: Web ---[references]---> Controller ---[references]---> Model Now I wanted to add 2 custom me...

C# Extensions

I typically use extension methods very sparingly. When I do feel compelled to write an extension method, I sometimes want to overload the method. My question is, what are your thoughts on extension methods calling other extension methods? Bad practice? It feels wrong, but I can't really define why. For example, the second CaselessIs met...

Raising C# events with an extension method - is it bad?

We're all familiar with the horror that is C# event declaration. To ensure thread-safety, the standard is to write something like this: public event EventHandler SomethingHappened; protected virtual void OnSomethingHappened(EventArgs e) { var handler = SomethingHappened; if (handler != null) handler(this, e)...

Evil use of extension methods?

Evil or not evil? public static void Raise(this EventHandler handler, object sender, EventArgs args) { if (handler != null) { handler(sender, args); } } // Usage: MyButtonClicked.Raise(this, EventArgs.Empty); // This works too! Evil? EventHandler handler = null; handler.Raise(this, EVentArgs.Empty); Note that due to t...

can you write extension methods on type arrays?

I need to write an extension method on a byte[]. Is that possible? ...