extension-methods

How do you make methods relying on extension methods testable?

I have an extension method with the following signature (in BuildServerExtensions class):: public static IEnumerable<BuildAgent> GetEnabledBuildAgents(this IBuildServer buildServer, string teamProjectName) { // Omitted agrument validation and irrelevant code var buildAgentSpec = buildServer.CreateBuildAgentSpec(teamProjectName);...

How do I use linq to get the difference of a List<byte[]> and an anonymous type with a byte[] property?

I am new to Linq and extension methods and I guess I just can't grasp the use of all the extension methods yet. I am reading a bunch of files, and after one has been read, I'm storing the hash value of the file into a database (the file names change, and they are moved around). Periodically, I want to check a directory and read any file...

F# and PLINQ extension methods

While digging deeper into the latest release of F# I tried to have it interacting with PLINQ. I've noticed, however, that the two don't play very nice together code-wise. In fact it didn't seem possible to write code such as the following: open System.Linq let someArray = [|"abc"; "def"|] someArray.AsParallel().Count(new Func<_,_>(fun ...

Is there an existing library of extension methods for C#? or share your own.

Possible Duplicate: Post your extension goodies for C# .Net (codeplex.com/extensionoverflow) I'm fond of C# 3.0. One of my favorite parts is extension methods. I like to think of extension methods as utility functions that can apply to a broad base of classes. I am being warned that this question is subjective and likely to ...

Psuedo-multiple-inheritance with extension methods on interfaces in C#?

Similar question but not quite the same thing I was thinking that with extension methods in the same namespace as the interface you could get a similar effect to multiple inheritance in that you don't need to have duplicate code implementing the same interface the same way in 10 different classes. What are some of the downsides of doin...

Problem creating my own extension to HtmlHelper

I have an extension method to HtmlHelper: <%= Html.MyMethod( params )%> It works in visual studio, but throws (at runtime): Compiler Error Message: CS0117: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'MyMethod' The odd bit is that this does work: <%= HtmlHelperExtensions.MyMethod( Html, params ) %> Why does ...

Mocking HttpPostedFileBase with Rhino Mocks

I'm new to mocking so I need a bit of guidance on how to mock HttpPostedFileBase with Rhino Mocks. I am trying to verify that my ToByteArray() extension works as expected and this is what I have so far: [Test] public void Should_return_a_byte_array_with_a_length_of_eleven() { // Arrange var stream = new MemoryStream(System.Text.En...

What is the best or most interesting use of Extension Methods you've seen?

I'm starting to really love extension methods... I was wondering if anyone her has stumbled upon one that really blew their mind, or just found clever. An example I wrote today: Edited due to other users' comments: public static IEnumerable<int> To(this int fromNumber, int toNumber) { while (fromNumber < toNumber) { yield...

Extension method repository

Extension methods are awesome and make programming so much more fun that I am constantly scouring the internet for new ones. Does anyone know if a web site exists that is dedicated to Extension methods, something similar to a code gallery but dedicated to C#/VB.NET Extension methods? ...

How do I properly write Math extension methods for int, double, float, etc.?

I want to write a series of Extension methods to simplify math operations. For example: Instead of Math.Pow(2, 5) I'd like to be able to write 2.Power(5) which is (in my mind) clearer. The problem is: how do I deal with the different numeric types when writing Extension Methods? Do I need to write an Extension Method for each t...

IoC (Castle Windsor) and Static Helpers

How do I configure Castle Windsor to use an Initialize method for a static helper class when requesting an object? I am trying to add some extension methods HtmlHelper so it has to be a static class and method. My HtmlHelper extensions depend on a IHtmlHelpersService that is configured with Castle Windsor already. I am using Convention O...

C#: SkipLast implementation

I needed a method to give me all but the last item in a sequence. This is my current implementation: public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source) { using (IEnumerator<T> iterator = source.GetEnumerator()) { if(iterator.MoveNext()) while(true) { ...

What does "this" mean when used as a prefix for method parameters?

I'm sure the answer is something obvious, and I'm kind of embarrassed that I don't really know the answer already, but consider the following code sample I picked up while reading "Professional ASP.NET MVC 1.0": public static class ControllerHelpers { public static void AddRuleViolations(this ModelStateDictionary modelState, IEnumer...

C# Extension methods on PocketPC Windows CE

Are extension methods available on CE framework as well? I have an extension method for string that works fine in a windows forms project, however it wont build in PocketPC application. I figured this would be an easy thing to find out, however I was unable to find any info regarding extension methods on PocketPC. Edit: Ooops this was ...

What's the cleanest way to render the results of a method while data binding?

The best way, of course, is to convert the method to a property. But I can't do that here -- We have an API from someone else, and we've added an extension method to one of the objects. We need at the string this method returns in a data-binding situation (a GridView). It doesn't seem that we can add an extension property (man, that ...

VB.Net Iniatialising a class using System.Reflection and System.Type to create a session based singlton extension method.

I have had several occasions recently to access a specific class several times over a relatively small time frame. So I've been storing the value of the class in Session and trying to access it on page load, if it's not available creating a new instance and storing that in session. So instead of constantly replicating the same code for...

Explicitly use extension method

I'm having a List<T> and want get the values back in reverse order. What I don't want is to reverse the list itself. This seems like no problem at all since there's a Reverse() extension method for IEnumerable<T> which does exactly what I want. My problem is, that there's also a Reverse() method for List<T> which reverses the list itse...

Get MethodInfo for Extension Method

I cant get the method info for an extension method as I would suspect. Whats wrong? _toStringMethod = typeof(ObjectExtensions).GetMethod("TryToString", BindingFlags.Public | BindingFlags.Static); ...

How can I write clean code for a class that uses modular extension methods?

I'm trying to do something rather... unique, and maybe there's a far better way to do it but... I'm doing an inversion of control(ish) system that uses extension methods to enable/disable components of the class, so before I get into more detail and confuse you, lets look at some code! using TestComponents.CommunicationProtocols.RS232; ...

Extension methods not showing

I am creating extension methods for the HtmlHelper class in an MVC web app. Nothing is showing, not even the default InputExtensions. public static class HtmlHelpers { public static void RegisterScriptInclude(this HtmlHelper htmlhelper, string script) { if (!RegisteredScriptIncludes.ContainsValue(script)) { ...