extension-methods

Passing interface as a parameter to an extension method

I have used extension methods to extend html helpers to make an RSS repeater: public static string RSSRepeater(this HtmlHelper html, IEnumerable<IRSSable> rss) { string result=""; foreach (IRSSable item in rss) { result += "<item>" + item.GetRSSItem().InnerXml + "</item>"; } ...

How do you reflect on a Linq extension method with a Func<TSource, bool> parameter?

I am trying to get a MethodInfo object for the method: Any<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) The problem I'm having is working out how you specify the type parameter for the Func bit... MethodInfo method = typeof(Enumerable).GetMethod("Any", new[] { typeof(Func<what goes here?, Boolean>) }); Help appreciated. ...

(Extension) Methods and Anonymous Types

Hi, I know that you cannot return anonymous types from methods but I am wondering how the Select extension method returns an anonymous type. Is it just a compiler trick? Edit Suppose L is a List. How does this work? L.Select(s => new { Name = s }) The return type is IEnumerable<'a> where 'a = new {String Name} ...

VB.NET Extension Methods

Hi, when I apply the tag above my methods I get the error Type System.Runtime.CompilerServices.Extension is not defined. Here is my sample <System.Runtime.CompilerServices.Extension()> _ Public Sub test() End Sub Where am I going wrong? Edit ~ Straight from the MSDN Article here, the same error Imports System.Runtim...

Does anyone know of a good article for creating MVC based components/ extension methods?

I've a couple of extension methods I've been developing for a couple of projects, they currently rely heavily on some AJAX to make bits and pieces work. The problem is that they require copying and pasting JavaScript files to the project you want to use it in. As this JavaScript file only needs to be used once (all instances of the rend...

.Net framework version in Silverlight: no List<T>.Find methods?

Today I discovered something that makes me sad: objects of type System.Generic.Collections.List don't have a number of the useful extension methods I've come to love, such as Find, FindAll, FindIndex, Exists, RemoveAll, etc. The object browser in VS2008 shows that those methods exist in the mscorlib version I'm using, but if I look at ...

Why doesn't VS 2008 display extension methods in Intellisense for String class

Since String implements IEnumerable<char>, I was expecting to see the Enumerable extension methods in Intellisense, for example, when typing the period in String s = "asdf"; s. I was expecting to see .Select<char>(...), .ToList<char>(), etc. I was then suprised to see that the extension methods do in fact work on the string class, the...

C# Extension Method Oddity during Unit Test

Using Visual Studio 2008 / C# / VS Unit Testing. I have a very straightforward extension method, that will tell me if an object is of a specific type: public static bool IsTypeOf<T, O>(this T item, O other) { if (!(item.GetType() is O)) return false; else return true; } It would be called like: Hashtable myHa...

Custom Linq Extension Syntax

I have written a function that gets a given number of random records from a list. Currently I can do something like: IEnumerable<City> cities = db.Cites.GetRandom(5); (where db is my DataContext connecting to a SQL Server DB) Currently, I have a function like this in every entity I need random records from: public partial class Cit...

LINQ to SQL auto-generated Extensibility Methods

When I generate entity classes using LINQ to SQL I get what I want but I get also a bunch of other Extensibility Methods Definitions. For Example for myField (TEXT) I get: partial void OnMyFieldChanging(string value); partial void OnMyFieldChanged(); What's a common use for the extensibility methods above? ...

Can i restrict this c# extension method definition with a WHERE keyword?

Hi folks, i'm trying to do the following extension method -> converting an int to a enum, when you provide the enum :- public static T ToEnum<T>(this int value) { return (T)Enum.ToObject(typeof(T), value); } Now, i was hoping to make it so that you can only define the type T to be an enumeration. Is there any what i can restrict ...

C# Extension Methods - How far is too far?

Rails introduced some core extensions to Ruby like 3.days.from_now which returns, as you'd expect a date three days in the future. With extension methods in C# we can now do something similar: static class Extensions { public static TimeSpan Days(this int i) { return new TimeSpan(i, 0, 0, 0, 0); } public static ...

How to call extension methods using Eval in a databound control

I have a simple extension method on the int type so I can do the following: string timeLength = 61.ToTime() // timeLength will be "1:01" This works great in code, but I want to use this extension method in a Repeater Template. When databinding I want to do the following: <%# Eval("LengthInSeconds").ToTime() %> That didn't work so I...

Using constrained generic extension methods in a separate assembly gives a reference error.

I created a separate assembly to contain common extension methods, the extension methods uses classes from System.Web.dll (and others). When I then create a new project (Console Application) that references the Utilities.dll assembly that contains the extension methods, I do not need to add a reference to System.Web.dll to the new proje...

Returning Array Fragments

I need to get array fragments from an array. I'm sick of using Array.Copy(). new ArraySegment(..).Array returns the original [full] array. The one below is what I came up with but I feel it's pretty lame. Is there a better way to do this? class Program { static void Main(string[] args) { var arr = new ArraySegment<b...

Select the maximal item from collection, by some criterion

i am new to .net 3.5. I have a collection of items: IList<Model> models; where class Model { public string Name { get; private set; } } I would like to get the element, which has the longest name's length. I tried string maxItem = models.Max<Model>(model => model.Name.Length); but it of course returns ...

What are Extension Methods?

What are extension methods in .NET? EDIT: I have posted a follow up question at Usage of Extension Methods ...

Usage of Extension Methods

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

How to properly document an extension method

So, I have a few extension methods, for commonly used stuff, and in documenting them, it's occurred to me that I have no idea how to consistently write the summary tag in the XML comments. For example: /// <summary> /// Gets a subset of characters from the left-hand side of a string. /// </summary> public static stri...

Where is the <%=Html.RadioButtonList()%> extension in MVC beta?

Is there any reason why there isnt a radiobutton list helper in the core MVC beta? There's a dropdownlist, but no radiobuttonlist. I know that I can easily create an extension method to do surely this is a core thing? ...