extension-methods

Where() at abstract level to return new self

I have this code and I want to keep it elegant. I got stuck at this inheriting issue and I would have to mess up the code if I do. Help me keep it elegant. I don't mind making changes anywhere up and down the hierarchy; feel free to change the core. I have these abstract classes (I omitted unrelated implementation to keep the question ...

How can I know items is in the enum?

In this question, I use xor operator between enum with [Flags] attribute as following: [Flags] enum QueryFlag { None = 0x1, ByCustomer = 0x2, ByProduct = 0x4, ByDate = 0x8 } QueryFlag flags = QueryFlag.ByCustomer | QueryFlag.ByProduct; To add an QueryFlag, of course we should use | operator. flags |= QueryFlag.ByDate; To re...

Can extension methods modify extended class values?

I was just trying to code the following extension method: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _4Testing { static class ExtensionMethods { public static void AssignMe(this int me, int value) { me = value; } } } But it is not wor...

How can I create extension methods with static return type?

Hi I was trying to write a simple extension method for Color static class which return Black and White equivalent of that color. The problem is that extention methods can't return Static types... So, how can I do this?! please help me. ...

Extension methods don't work on subclasses?

Apparently, extension methods don't work on subclasses, or is it just me? private class Parent { } private class Child { } public static class Extensions { public static void Method(this Parent parent) { } } //Test code var p = new Parent(); p.Method(); // <--- compiler like var c = new Child(); c.Metho...

Add An Anchor To RedirectToAction's Result?

I'm trying to create an extension method similar to MVCContrib's RedirectToAction method by creating a method that will take an #anchor argument and add it to the Url. I am familiar with this question but it's not strongly typed. It's also possible to add values to the query-string, but that won't work for an anchor. public static Redir...

Using SelectMany Extension Method to Select Multiple Columns from DataRow List with result as subset of datarow list

I have a datatable containing 10 columns. I want to select only two columns of them. I am not able to do it using SelectMany Extension Method. I know how to get it from Linq To DataSet but trying using this extension method. ...

Is it a good pratice to make an extension method that never use the extended object?

In our ASP.NET MVC project, we have an HtmlHelper extension method to generate a static google map. public static MvcHtmlString StaticMap(this HtmlHelper helper, string address, string alt, int width, int height, int zoom) { var src = new Uri("http://maps.google.com/maps/api/staticmap?markers=size:mid|color:red|{0}&amp;zoom={1}&amp;...

How to Mock (with Moq) Unity methods

Extension methods are not good for testing (that's described here: http://stackoverflow.com/questions/2295960/mocking-extension-methods-with-moq, http://www.clariusconsulting.net/blogs/kzu/archive/2009/12/22/Howtomockextensionmethods.aspx). But probably there are some solutions for mocking of Unity methods? In my case I have the followi...

how to implement left join in JOIN Extension method in C#

I am trying to implement outer join on this kind of query for p.Person table. How to do it. this eg is taken from http://ashishware.com/DSLinqExample.shtml var onlyinfo = p.Person.Where(n=>n.FirstName.Contains('a')).Join(p.PersonInfo, n => n.PersonId, ...

How do I ensure a sequence has a certain length?

I want to check that an IEnumerable contains exactly one element. This snippet does work: bool hasOneElement = seq.Count() == 1 However it's not very efficient, as Count() will enumerate the entire list. Obviously, knowing a list is empty or contains more than 1 element means it's not empty. Is there an extension method that has this ...

Extension "WhenNull" to check for null and init the underlying object via lambda

Hi, I was asked whats wrong/how can the following scenario can be fixed Customer customer = null; customer.WhenNull(c => new Customer()) .Foo(); // instead of Customer customer = null; if (customer == null) { customer = new Customer(); } customer.Foo(); One developer sends me his Version of the WhenNull extension public ...

Defining my own Where-Method for LINQ to Objects - How do I know which one will get used?

Hi everyone, Just for testing reasons, I defined my own Where-Method for Linq like so: namespace Test { public static class LinqTest { public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate) { ...

Objective-c class extensions not effectives in static library

Hello, while creating a library that will be used on several projects, I encountered an error that I was not able to resolve by myself. The library is composed of several "modules" that each declares its set of classes. The modules declares a header file that references the classes. Each module header is included in the library header,...

Help me with my first Mozilla Extension.

I am new to Mozilla extensions and i have been trying to build the "hello world" following this tutorial https://developer.mozilla.org/en/Building_an_Extension but unfortunately its not showing anything on the right of my status bar when i start Firefox with my development profile. I have checked everything many times and not found any m...

How to use IQueryable types in DataGridViewColumnCollection in WinForm

I want to set DataGridView's columns to ReadOnly except one or two columns. I was trying to do this. dgv.Columns.AsQueryable().ForEach(col=>col.ReadOnly=true); But I found that these extension methods are not available at DataGridViewColumnCollection How can I do the same by this way ...

C# - Help with [Flags] Enum & Extension Method

Hi Guys, I have the following enum: [Flags] public enum PostAssociations { None = 0x0, User = 0x1, Comments = 0x2, CommentsUser = 0x3 } As a starting note, im not sure if those flags are correct. I'm doing this so that i have a fluent way of defining "Includes" for Entity Framework (as the EF Include method takes a s...

Linq - Is there a IEnumerable.ForEach<T> extension method that I'm missing?

Possible Duplicates: Lambda Expression using Foreach Clause Why is there not a ForEach extension method on the IEnumerable interface? This seems pretty basic. I'm trying to iterate over each object of an IEnumerable. It appears that I would have to cast it to a list first. Is that right? It seems to me there should be an ext...

Can you add extension methods that you call like static methods?

According to Microsoft, "Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type". Is there a way to add an extension method that it called as if it was a static method? Or to do something else that has the same effect? Edit: By which I mean "called as if it was a...

extension method for a method C#

Hi everyone, Is there a way to have extension method on the method? Example for this would be method that takes some user object as parameter and you need to do security check if that user can use that method at the very beginning of the method. Can the method have extension method like "check can this user use me" and return bool. Th...