extension-methods

ASP.NET MVC: 'Extend' Html.TextBox to conditionally have CSS value?

Hello, I would like to extend ASP.NET MVC's TextBox so it conditionally switches its CSS value (based on data from the Model). However, I don't want to put that logic in my view (by conditionally changing htmlAttributes/via method call there), but instead extend the class so I don't clutter my view. Basically I want the TextBox on creati...

Extending string with Extension Methods (C# 3.0)?

I know I can extend the string class like so: public static class EMClass { public static int ToInt32Ext(this string s) { return Int32.Parse(s); } public static int ToInt32Static(string s) { return Int32.Parse(s); } } And use it like this: string x = "12"; x.ToInt32Ext() But how can I make it to where ...

Do Extension Methods Hide Dependencies?

All, Wanted to get a few thoughts on this. Lately I am becoming more and more of a subscriber of "purist" DI/IOC principles when designing/developing. Part of this (a big part) involves making sure there is little coupling between my classes, and that their dependencies are resolved via the constructor (there are certainly other ways o...

Convert string[] to int[] in one string of code using LINQ

I have an array of integers in string form: var arr = new string[] { "1", "2", "3", "4" }; I need to an array of 'real' integers to push it further: void Foo(int[] arr) { .. } I tried to cast int and it of course failed: Foo(arr.Cast<int>.ToArray()); I can do next: var list = new List<int>(arr.Length); arr.ForEach(i => list.Add...

Distinct() with lambda?

Right, so I have an enumerable and wish to get distinct values from it. Using System.Linq, there's of course an extension method called Distinct. In the simple case, it can be used with no parameters, like: var distinctValues = myStringList.Distinct(); Well and good, but if I have an enumerable of objects for which I need to specify ...

Auto implementing looping

I don't know if the title makes sense, but in the app I am writing there are lots of (extension) methods. A simple example: Objects: Matter (Burn, Explode, Destroy, Freeze, Heat, Cool) Atom (Attach, Detach) <many more> And a custom collection like: ImmutableList<T> And methods like these: public static class Burner { public s...

Can I extend the operators that LINQ-to-SQL supports?

If I wanted to badly enough, could I add additional LINQ constructs to LINQ-to-SQL (as I would be able to if creating my own LINQ provider)? For instance, many of the built-in LINQ operators (XYZ.Any()) get directly translated to SQL (e.g. IF EXISTS(XYZ) ). If I needed a specialized construct, would I even be able to augment the set, o...

Adding Methods to Nullable Types

We have come across a scenario where we need to track the setting and unsettling of a nullable type. so something like int? value if(value.isSet()) { addTocollection(); } we would also need a clear function value.clear(); The concept is that the data has an extra state that is the set state. so NULL(set) and NULL(unset) have...

Use system namespaces for class libraries: good or bad

Hi, Is it a good idea to use "system namespaces" in my class libraries? Sample: namespace System.Web { public static class RequestExtensions { public static bool IsPost(this HttpRequest r) { return string.Compare(r.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase) == 0; } } } The advantage...

Enumerable.Except does not use my custom comparer

Hello folk, I try to use the except method with a custom equality comparer, but it is not work. My equality comparer: public class BusinessObjectGuidEqualityComparer<T> : IEqualityComparer<T> where T : BusinessObject { #region IEqualityComparer<T> Members /// <summary> /// Determines whether the specified objects are equa...

How do I turn off the "Convert Extension Method to Plain Static" automatic refactoring in resharper?

When using Resharper, for some reason, when I call an extension method, it automatically converts it into a static method call. This is the so called Convert Extension Method to Plain Static refactoring. foo.Bar() becomes MyStaticExtensions.Bar(foo); Ironically, it then flags this as a code smell. How do I turn this off? ...

Modifying the String, Date and Number object in JavaScript

I'm working on a project which makes heavy use of extension methods to convert strings from the UI-layer into their appropriate object-types in the code-layer: (pseudo-code) // C# /* Converts a String from the UI-layer, formatted according to a user-defined UI-culture preference (in this case from da-DK) into a Double */ Double d = "1...

Replace nested ForEach with Select if applicable

Is it possible to replace method ForEach() usage with Select() or something else to write next code in one string with nested extension methods? OR maybe there are another ways to improve the algorithm? var list = new List<IStatementParser>(); System.IO.Directory.GetFiles(path, "*.dll") .ForEach(f => System.Reflection.Assembly.Load...

Object Extension Methods on Value Types

Hi, I have an Extension Method: public static string ToDelimenatedString(this object[] array, string delaminator) {...} The Extension is applied to reference types but not value types. I assume this is because object is nullable. How would I write the method above to target value types, is it even possible without writing it out for ...

How to add custom columns to a table that LINQ to SQL can translate to SQL

I have a table that contains procedure codes among other data (let's call it "MyData"). I have another table that contains valid procedure codes, their descriptions, and the dates on which those codes are valid. Every time I want to report on MyData and include the procedure description, I have to do a lookup similar to this: From m in ...

Modify ValueType from extension method?

Hi :) A few days ago i needed to toggle a bool, and i ended up doing like so: IsVisible = !IsVisible; I found that to be the simplest way to archive that functionaily. But before doing like the example above, i tried out some different ways. Mostly about using a extension method. Which in my opinon would make it even simplier, or at...

Overriding ToString() of List<MyClass>

Hello. I have a class MyClass, and I would like to override the method ToString() of instances of List: class MyClass { public string Property1 { get; set; } public int Property2 { get; set; } /* ... */ public override string ToString() { return Property1.ToString() + "-" + Property2.ToString(); } } I w...

Why is the value or constructor 'handler' not defined?

I tried to use object expression to extend the IDelegateEvent, but in fsi there was an error FS0039: The value or constructor 'handler' is not defined. My codes are as follows: type IDelegateEvent<'Del when 'Del:> Delegate> with member this.Subscribe hanlder = do this.AddHandler(handler) { new IDisposable with membe...

Should I bother throwing an exception from this method?

I'm performing some parameter validation in a method and throwing an exception where necessary. Do I need to bother manually throwing exceptions of this type? As long as the caller is wrapped in a try..catch block a similar exception is thrown regardless of whether the manual checks are in place. public static Int16 ToInt16(this b...

Is it bad practice to subscribe to events in a C# Extension Method?

In this case, is it bad to subscribe to the proxy CloseCompleted event? public static void Close(this MyWCFServiceClient proxy) { proxy.CloseCompleted += (o, e) => { if (e.Error != null) proxy.Abort(); }; proxy.CloseAsync(); } when the proxy is no lon...