extension-methods

In C or C++, is there a way to extend a class without inheritance?

Is there a way to implement functionality like Class Categories (of Objective-C) or Extension Methods (of C# 3.0) in C and/or C++? ...

C# reflection instantiation

I am currently trying to create a generic instance factory for which takes an interface as the generic parameter (enforced in the constructor) and then lets you get instantiated objects which implement that interface from all types in all loaded assemblies. The current implementation is as follows: public class InstantiationFactory<T> ...

Deserialize Stream to List<T> or any other type

Attempting to deserialize a stream to List<T> (or any other type) and am failing with the error: The type arguments for method Foo.Deserialize<T>(System.IO.Stream) cannot be inferred from the usage. Try specifying the type arguments explicitly. This fails: public static T Deserialize<T>(this Stream stream) { BinaryFormatter bi...

Where's IEnumerable<T>.OrderBy declared?

Sorry for being lazy but where's IEnumerable.OrderBy declared. I know it's an extension method but where, in which class is it declared? PS: Opening reflector... ...

Create IEnumerable<T>.Find()

I'd like to write: IEnumerable<Car> cars; cars.Find(car => car.Color == "Blue") Can I accomplish this with extension methods? The following fails because it recursively calls itself rather than calling IList.Find(). public static T Find<T>(this IEnumerable<T> list, Predicate<PermitSummary> match) { return list.ToList().Find(match...

How to resolve ambigiously named extension method?

I have a DataTable that I'm trying to enumerate over with the AsEnumerable extension method on System.Linq.Enumerable. The problem is that there is an identically named extension method on System.Data.DataTableExtensions. I need to use both namespaces in my class so removing one of the using statements is not an option. How do I decl...

About extension methods

Shall i always need to throw ArgumentNullException(well,extension methods in Enumerable throw ArgumentNullException) when an extension method is called on null?I would like to have a clarification on this?If the answer is an Yes and No please present both the cases. ...

How extension methods work in background ?

I am just cuirous about behind of extension method mechanism.Some questions and answer appear in my mind. MyClass.OrderBy(x=>x.a).OrderBy(x=>x.b); I was guessing that mechanism was first orderby method works and order them by a member then returns sorted items in IEnumarable interface then next Orderby method of IEnumarable Order them...

Extending EF4 SQL Generation

Hi, We're using EF4 in a fairly large system and occasionally run into problems due to EF4 being unable to convert certain expressions into SQL. At present, we either need to do some fancy footwork (DB/Code) or just accept the performance hit and allow the query to be executed in-memory. Needless to say neither of these is ideal and th...

Extension method not working if I set controller property in Action, works in OnExecuting

I have a class MyController that inherits from Controller, so all my controllers inherit from MyController. I have a property in MyController: public class MyController : Controller { public string SomeProperty {get;set;} } if I set this property in MyController's OnExecuting method, my HtmlHelper extension method works fine: pu...

Behaviour to simulate an enum implementing an interface

Say I have an enum something like: enum OrderStatus { AwaitingAuthorization, InProduction, AwaitingDespatch } I've also created an extension method on my enum to tidy up the displayed values in the UI, so I have something like: public static string ToDisplayString(this OrderStatus status) { switch (status) { ...

Extending Enums, Overkill?

I have an object that needs to be serialized to an EDI format. For this example we'll say it's a car. A car might not be the best example b/c options change over time, but for the real object the Enums will never change. I have many Enums like the following with custom attributes applied. public enum RoofStyle { [DisplayText("Gl...

Is there any extension method which get void returning lamda expression ?

For example int[] Array = { 1, 23, 4, 5, 3, 3, 232, 32, }; Array.JustDo(x => Console.WriteLine(x)); ...

Prefer extension methods for encapsulation and reusability?

edit4: wikified, since this seems to have morphed more into a discussion than a specific question. In C++ programming, it's generally considered good practice to "prefer non-member non-friend functions" instead of instance methods. This has been recommended by Scott Meyers in this classic Dr. Dobbs article, and repeated by Herb Sutter a...

Why use TagBuilder instead of StringBuilder?

what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable? aren't they generating the same thing?? ...

Visual Studio Code Analysis Rule - "Do not expose generic lists"

Do not expose generic lists IF all my methods, need to expose a collection, then I need to user the Linq Extension .ToList(), almost everywhere I need to use lists, or user Collections in all my code. If that’s the case, .ToList() is ignoring the rule right? Or is there a technique like copying the list o something to fix the violation...

Error message in XSLT with C# extension function

Hi! I am received the following error while trying to implement a C# extension function in XSLT. Extension function parameters or return values which have CLR type 'Char[]' are not supported.** code: <xsl:variable name="stringList"> <xsl:value-of select="extension:GetList('AAA BBB CCC', ' ')"/> </xsl:variable> <msxsl:script l...

Possible to write an Extension Method for ASP.NET's Html.ActionLink() method?

Right now, I'm trying to work around an IE6/7 bug which requires the wrapping of the </a> closing tag with this IE specific comment to make some drop-down menu work: <!--[if IE 7]><!--></a><!--<![endif]--> Unfortunately, I cannot inject this directly into my View page code like this: <%= Html.ActionLink("LinkName<!--[if IE 7]><!--></...

Generic wrapper for System.Web.Caching.Cache functions

I've created a generic wrapper for using the Cache object: public class Cache<T> where T : class { public Cache Cache {get;set;} public CachedKeys Key {get;set;} public Cache(Cache cache, CachedKeys key){ Cache = cache; Key = key; } public void AddToCache(T obj){ Cache.Add(Key.ToString(), ...

MenuItemCollection vs. List<MenuItem>

Recently I wrote a piece of C# code utilizing a Lambda expression: var dynMenu = new List<MenuItem>(); // some code to add menu items to dynMenu if (!dynMenu.Any(x => x.Text == controller)) { // do something } While going thru my code, I discovered that each MenuItem itself has a property called ChildItems which happens to be of ...