extension-methods

Apply C# Attribute to Extension Method to Manage Extension Method Execution like an Action Filter

Hi All I have a HtmlHelper extension method that I would like to apply some logic to before execution. I suppose I'm looking for behaviour similar to Action Filters, abut I thought I could do this by applying an Attribute to a method without the need of a Filter Context. This is my extension method: [MyHelperAttribute] public static s...

How to compare nullable types?

I have a few places where I need to compare 2 (nullable) values, to see if they're the same. I think there should be something in the framework to support this, but can't find anything, so instead have the following: public static bool IsDifferentTo(this bool? x, bool? y) { return (x.HasValue != y.HasValue) ? true : x.HasValue && x...

How to call a generic extension method dynamically?

I wrote this extension method: public static DataTable ToDataTable<T>(this IList<T> list) {...} It works well if called with a type known at compile time: DataTable tbl = new List<int>().ToDataTable(); But how to call it if the generic type isn't known? object list = new List<int>(); ... tbl = Extension.ToDataTable((List<object>)l...

Creating a large form with multiple dropdowns and text fields in ASP.NET MVC

In my continuing journey through ASP.NET MVC, I am now at the point where I need to render an edit/create form for an entity. My entity consists of enums and a few other models, created in a repository via LINQtoSQL. What I am struggling with right now is finding a decent way to render the edit/create forms which will contain a few dro...

Effective weight (at both design time, runtime, etc...) of extension methods in .NET

I was wondering what is the effective weight of extension methods, if used very wildly. If, for example, i decide to create a lot of string/int/datetime extension methods for many of the common day-to-day tasks, to obtain a kind of fluent interface as a result, it may be that the overall weight would pump up excessively? And if yes, wou...

Have you ever used private extension methods?

Is there any advantage of having the option of using private extension methods? I haven't found any use for them whatsoever. Wouldn't it be better if C# didn't allow them at all? ...

Linq where keyword vs. Where extension and Expression parameters

Hi, Passing in an Expression to a Linq query behaves differently depending on syntax used, and I wonder why this is the case. Let's say I have this very generic function private IEnumerable<Company> GetCompanies(Expression<Func<Company, bool>> whereClause) The following implementation works as expected private IEnumerable<Comp...

Silverlight Extension Methods in Class Library

I've implemented some extension methods as defined here. I've put these into a separate assembly - Brandon.Extensions. It builds against the Silverlight runtime. I have a Silverlight application in which I would like to use these extension methods. I have added a reference to the Brandon.Extensions project from my Silverlight project - n...

Extending the base class

When you create a webpage in ASP.net, the codebase inherits System.Web.UI.Page Now when I extend the page object to check for SessionExpired for example, I create a new class and inherit from system.web.ui.page, and overwrite some functions. The problem then is that on each page, I have to replace system.web.ui.page with my custompage ...

How to write generic IEnumerable<SelectListItem> extension method

I'm fairly new (ok, REALLy new) to generics but I love the idea of them. I am going to be having a few drop-down lists on a view and I'd like a generic way to take a list of objects and convert it to a list of SelectListItems What I have now: public static IEnumerable<SelectListItem> ToSelectListItems( this IEnumerable<SpecificOb...

Please explain extension methods to me

I just looked at this posting: What is the best or most interesting use of Extension Methods you’ve seen? I've never heard of extension methods. Do they apply to every language? What is the point of them? In that particular posting I did not understand the example. ...

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

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

Why can't I create extension methods for static classes?

When I try to create an extension method for the File class, I get an error telling me that I cannot do this because the class is static. However, I don't see why this stops the creation of an extension method, what implication is there? Thanks ...

Why does this extension method throw a NullReferenceException in VB.NET?

From previous experience I had been under the impression that it's perfectly legal (though perhaps not advisable) to call extension methods on a null instance. So in C#, this code compiles and runs: // code in static class static bool IsNull(this object obj) { return obj == null; } // code elsewhere object x = null; bool exists = !...

C#: Is this a reasonable extension method for an ASP.NET project?

I recently found out about C# extension methods and wrote this one: /// <summary> /// Short-hand for setting the data source and binding it. /// </summary> /// <param name="me">The control to set and bind the data source of.</param> /// <param name="dataSource">The data source.</param> public static void BindTo(this DataBoundControl me,...

Using Generics to Create HtmlHelper Extension Methods

Hi Guys I'm not really familiar with creating generic methods, so I thought I'd put this question to the community & see what comes back. It might not even be a valid use of generics! I'd like to create a HtmlHelper extension method where I can specify that the method is of a certain type. I pass into the method an instance of that typ...

Good practice to create extension methods that apply to System.Object?

Hello, I'm wondering whether I should create extension methods that apply on the object level or whether they should be located at a lower point in the class hierarchy. What I mean is something along the lines of: public static string SafeToString(this Object o) { if (o == null || o is System.DBNull) return ""; else { ...

How do I apply a String extension to a Func<String>

Hi All, I have a constructor signature like this public NavigationLink(Func<String> getName, Func<UrlHelper, String> getURL, Func<bool> isVisible, IEnumerable<NavigationLink> subItems) Inside that constructor I am assigning the getName to a property of the containin...

Extension method on enumeration, not instance of enumeration

I have an enumeration for my Things like so: public enum Things { OneThing, AnotherThing } I would like to write an extension method for this enumeration (similar to Prise's answer here) but while that method works on an instance of the enumeration, ala Things thing; var list = thing.ToSelectList(); I would like it to work on...

Create Extension Method to Produce Open & Closing Tags like Html.BeginForm()

Hi Guys I wonder if it's possible to create an extension method which has functionality & behaviour similar to Html.BeginForm(), in that it would generate a complete Html tag, and I could specificy its contents inside <% { & } %> tags. For example, I could have a view like: <% using(Html.BeginDiv("divId")) %> <% { %> <!-- Form con...