extension-methods

C#: Discovering Extension Methods

What tools or techniques do you recommend for discovering C# extension methods in code? They're in the right namespace, but may be in any file in the solution. Specifically: How can I find all extension methods (and navigate to them) on the current type in the code window? I do have Resharper (v4), so if that has a mechanism I'm no...

Extension method vs parent class method behaviour

Look at the following code: class A { public string DoSomething(string str) { return "A.DoSomething: " + str; } } class B : A { } static class BExtensions { public static string DoSomething(this B b, string str) { return "BExtensions.DoSomething: " + str; } } class Program { static void Mai...

C# - Deserialize a List<String>

I can serialize a list really easy: List<String> fieldsToNotCopy =new List<String> {"Iteration Path","Iteration ID"}; fieldsToNotCopy.SerializeObject("FieldsToNotMove.xml"); Now I need a method like this: List<String> loadedList = new List<String(); loadedList.DeserializeObject("FieldsToNotMove.xml"); Is there such a method? Or am...

C# Ambiguous calls - Different return type

Hello, I have 2 extension methods that convert a MongoDB document to an entity/object. public static ProductTemplate Convert(this Document document) { return null; } public static Product Convert(this Document document) { return null; } This gives an expected ambiguous call error so I was wondering how I could fix this? Fri...

How can I make this extension method more generic?

I'm having a brain fart trying to make the following method more generic such that any List<T> can be passed in for the columnValues parameter. Here's what I have: public static DataRow NewRow(this DataTable dataTable, List<string> columnValues) { DataRow returnValue = dataTable.NewRow(); while (columnValues.Count > returnValue...

Using Extension Methods from within an Object's constructor where "Me" is the ByRef target object.

Consider the following: Public Module Extensions <Extension()> _ Public Sub Initialize(ByRef Target as SomeClass, ByVal SomeParam as Something ) ... Target = SomethingElse end Sub End Module Class SomeClass ... sub New(ByVal SomeParam as Something ) Me.Initialize(SomeParam) end sub sub ...

How to convert an IEnumerable<IEnumerable<T>> to a IEnumerable<T>

I have an IEnumerable<IEnumerable<T>> collection that I want to convert to a single dimension collection. Is it possible to achieve this with a generic extension method? Right now I'm doing this to achieve it. List<string> filteredCombinations = new List<string>(); //For each collection in the combinated results collection foreach (var...

Why is this extension method not working?

I can't get to do what I want. I only want accounts for non-international representatives. But when I call ActiveAccounts(), I'm not getting null, I'm getting an enumerable, which then includes null. What am I doing wrong here? Help please. public static class AccountExt { public static IEnumerable<Account> ActiveAccounts( this Acco...

What for System.Web.Extensions ?

By default ASP.NET 3.5 Web App project has references to assemblies System.Data.DataSetExtensions.dll and System.Web.Extensions.dll. With System.Data.DataSetExtensions everything is clear for me, it contains such useful extension methods as DataTable.AsEnumerable() and DataTable.CopyToDataTable(). And what about System.Web.Extensions.d...

Is it possible to create Extension Methods with 2.0 Framework?

I was wondering if there is a way to create extension methods using Visual Studio 2005 and the 2.0 framework? public static class StringExtensions { public static void SomeExtension(this String targetString) { } } If there is no way to do this, what would the equivalent be? Just create static methods in some sort of libra...

Why is Html.DropDownList() producing <select name="original_name.name"> instead of just <select name="name">?

I have an editor template whose job is to take a SelectList as its model and build a select element in html using the Html.DropDownList() helper extension. I'm trying to assign the name attribute for the select based on a ModelMetadata property. (Reason: on post-back, I need to bind to an object that has a different property name for th...

Extension Method for Generic Class.

Possible Duplicates: C# -Generic Extension Method How do you write a C# Extension Method for a Generically Typed Class Is it possible to declare extension methods for generic classes? public class NeedsExtension<T> { public NeedsExtension<T> DoSomething(T obj) { } } ...

Mocking Extension Methods with Moq

I have a preexisting Interface... public interface ISomeInterface { void SomeMethod(); } and I've extended this intreface using a mixin... public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface someInterface) { // Implementation here } } I have a class thats callin...

List<T>.ForEach vs. custom IEnumerable<T> extension

Say I have a class: public class MyClass { ... } and a webservice method that returns an IEnumerable<MyClass> The consumer of the webservice defines some method: public void DoSomething(MyClass myClass) { ... } Now, the consumer can call DoSomething on the result of the webservice method in two ways: var result = // web ser...

If an extension method has the same signature as a method in the sealed class, what is the call precedence ?

I was reading about extension methods in C# 3.0. The text I'm reading implies that an extension method with the same signature as a method in the class being extended would be second in order of execution - that is, the method in the sealed class gets called. If this is the case, how can you extend the sealed class ? ...

Using ms: xpath functions inside XPathExpression

I am trying to use Microsoft XPath Extension Functions (such as ms:string-compare http://msdn.microsoft.com/en-us/library/ms256114.aspx) inside an XPathExpression object. These functions are extensions inside the MSXML library, and if I use them in an XslCompiledTransform (simply adding the "ms" namespace) they work like a charm: ...

Extension method to filter a list of concrete types based on interface?

I have a multiple classes that implement an interface. I want to create an extension method to filter a list of these classes based on a value. Here's a hypothetical example: public enum Transmission { Standard = 1, Automatic = 2 } public interface ICar { Transmission Tranny { get; set; } } public class Car : ICar { ...

Alternative to being able to define static extension methods

I understand that I can't extend static classes in C#, I don't really understand the reason why, but I do understand that it can't be done. So, with that in mind, here is what I wanted to achieve: public static class GenericXmlSerialisationExtender { public static void WriteToXML<T>(this T targetObject, string fileName) { ...

How can I get an extension method to change the original object?

I want to be able to write extension methods so that I can say: lines.ForceSpaceGroupsToBeTabs(); instead of: lines = lines.ForceSpaceGroupsToBeTabs(); However, the following code currently outputs: ....one ........two instead of: Tone TTtwo What do I have to change in the following code to make it output: Tone TTtwo (not...

How do C# Linq extension methods perform equality comparison?

So, the following lambda expression is not returning any elements in the collection, even though while stepping through I was able to verify that 1 item matches the criteria. I've added a sample of the class with it's IEquatable implementation. ...within a method, foo is a method parameter var singleFoo = _barCollection.SingleOrDefault(...