extension-methods

linq to sql OnLoaded() with SQL View?

I am trying to extend my Linq-to-Sql entity with a few extra properties. These are "calculated" properties based on data from the underlying SQL View. For example, think of having a Date of Birth field, which is used to calculate an extended Age field. I tried to extend my entity class by extending the OnLoaded() method. I get a comp...

Writing Extension methods with Action / Func / Delegates / Lambda in VB and C#

hey guys. Firstly I can't get my head around the functional / Lambda aspects of .NET 3.5. I use these features everyday in LINQ, but my problem is understanding the implementation, and what they really mean (Lambda? System.Func? etc etc) With this in mind, how would the following be achieved: As an example, I'd like to have an Extensi...

PowerShell, Extension Methods, and Monkey Patching

Is it possible to write extension method in PowerShell? or to bolt a new method on top of an existing type like [string] live at runtime? ...

What are your favorite custom extension methods?

Duplicate: Post your extension goodies for C# .Net (codeplex.com/extensionoverflow) Let's create a list of your favorite extension methods. To qualify it should be an extension method you use often and makes coding easier by being either elegant, clever, powerful or just very cool. I'll start with my 3 favorite extension methods...

For each get RowIndex

Is there a way in a for each to get the row index ? Example : int rowIndex = 0; foreach (int a in numbers) { // Manipulation rowIndex++; } What I would like to have foreach (int a in numbers) { a.RowIndex; } Is there a quick way of doing it ? Maybe with the use of extension methods ? ...

In C#, what happens when you call an extension method on a null object?

Does the method get called with a null value or does it give a null reference exception? MyObject myObject = null; myObject.MyExtensionMethod(); // <-- is this a null reference exception? If this is the case I will never need to check my 'this' parameter for null? ...

ASP.NET repeater alternate row highlighting without full blown <alternatingitemtemplate/>

I'm trying to accomplish simply adding a css class to a div on alternate rows in my <itemtemplate/> without going to the overhead of including a full blown <alternatingitemtemplate/> which will force me to keep a lot of markup in sync in the future. I've seen a solution such as http://blog.net-tutorials.com/2009/04/02/how-to-alternate-r...

Instantiating an instance of a class using an extension method

The idea behind this code is that it uses an extension method to instantiate an instance of a class if that class is set to null. The code is simple enough, but does not work (it compiles and runs, but at the end the object reference is still null). Can anyone suggest why? The following code uses a simple class SomeClass that has a sin...

How I can find all extension methods in solution ?

How I can find all extension methods in solution ? ...

Static extension methods

Is there any way I can add a static extension method to a class. specifically I want to overload Boolean.Parse to allow an int argument. ...

How to make this extension method compile?

I am trying to create an extension method for the generic delegate Action<T> to be able to make simple asynchronous calls on Action<T> methods. It basically just implements the pattern for when you want to execute the method and don't care about it's progress: public static class ActionExtensions { public static void AsyncInvoke<T>(...

Html Helper Extensions not being found

I'm using the release version of ASP.net MVC and I seem to be getting this error a lot 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'RenderPartial' and no extension method 'RenderPartial' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly refer...

VB.NET Extension Method in View using ASP.NET MVC

I ran into a strange issue over the weekend while I was working on an asp.net mvc project in vb.net. I created an extension method to convert an integer to the corresponding month it is associated with. I tested the extension method in a console application so I know it is working. In my asp.net mvc project I have a view and want to...

My first extension method, could it be written better?

Because this is my first attempt at an extension method that seems quite useful to me, I just want to make sure I'm going down the right route public static bool EqualsAny(this string s, string[] tokens, StringComparison comparisonType) { foreach (string token in tokens) { if (s.Equals(token, comparis...

Is there any way in C# to override a class method with an extension method?

There have been occasions where I would want to override a method in a class with an extension method. Is there any way to do that in C#? For example: public static class StringExtension { public static int GetHashCode(this string inStr) { return MyHash(inStr); } } A case where I've wanted to do this is to be abl...

Is there a way to write the canonical OnPropertyChanged method as an extension method?

basically I want to do something like this: public static void OnPropertyChanged(this INotifyPropertyChanged changedObject, string propertyName) { var handler = changedObject.PropertyChanged; if (handler != null) { var e = new PropertyChangedEventArgs(propertyName); handler(changedObject, e); } } which ...

Is there any way to overload extension methods in C#?

I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS : PARENTCLASS{...} public static class EXTENSION{ public static METHOD(this PARENTCLASS parent){...} public static METHOD(this CHILD_A_CLASS child) {...} } Something like above, of course there will be more child (and grandchild)...

C# Extension Method not working for increment and decrement scenarios, is it by design?

Hi I am trying a simple Extension Method example and am unable to increment or decrement an int. Here is the code static class ExtensionMethodsExp { public static void Print(this object o) { Console.WriteLine("This is print: {0}", o.ToString()); } public static int Double(this int i) { return i * 2;...

Extension method for Dictionary of Dictionaries

I'm trying to write an extension method to insert data into a dictionary of dictionaries defined as follows: items=Dictionary<long,Dictionary<int,SomeType>>() What I have so far is: public static void LeafDictionaryAdd<TKEY1,TKEY2,TVALUE>(this IDictionary<TKEY1,IDictionary<TKEY2,TVALUE>> dict,TKEY1 key1,TKEY2 key2,TVALUE value) ...

MediaWiki recursiveTagParse returns false

I am writing an extension for internal use in my group. It takes some parameters and should return a table of DRs for the release we are working on. It queries our ClearQuest, and its all working great. However, when more than 606 (a common occurrence) rows are in the table no output is returned at all. I write the table using wiki m...