Questions tagged [extension-methods]

An extension method is a language feature of some languages, such as Swift, Visual Basic.NET and C#. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

An Extension method is a new language feature of C# starting with the 3.0 specification, as well as Visual Basic.NET starting with 9.0 and Oxygene with 2.0, and is a fundamental aspect of Swift. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

3545 questions
2
votes
2 answers

EF query using extension method inside extension method

I have table Requests, that has Approvals : public class Request { public virtual List Approvals { get; set; } } Requests come from DB public DbSet Request { get; set; } I have extension method: public static…
Jaanus
  • 16,161
  • 49
  • 147
  • 202
2
votes
1 answer

Overloading a generic extension method

I have a problem overloading an extension method. I have two extension Methods: Method A - For standard objects: public static bool HasChanged(this T obj1, T obj2, Func equalityExpression) Method B - For IEnumerables: public static…
Jannik
  • 2,310
  • 6
  • 32
  • 61
2
votes
2 answers

How to use C# extension methods with SmartFormat reflection syntax?

Is it possible to make the following example work with SmartFormat.NET? void Main() { Dictionary ps = new Dictionary(); ps["Name"] = "Niels"; Smart.Format("{Name.Foo} is my name", ps).Dump(); …
Niels Bosma
  • 11,758
  • 29
  • 89
  • 148
2
votes
2 answers

C# Extension Method Not Seen

I know this is a silly mistake, but I can't figure out what's going on. I've created some extension methods and am trying to access them, but the default methods keep getting called: namespace MyProject { public static class Cleanup { …
Bryant
  • 355
  • 9
  • 21
2
votes
3 answers

MVC 5 - Redirect to a controller from anywhere

I am developing a MVC 5 internet application, and I wish to redirect to a controller/action result from anywhere in my application. The controller name is "Manager", and the action result is "TestAction" In an action result, the following code can…
Simon
  • 7,991
  • 21
  • 83
  • 163
2
votes
2 answers

C# ambiguity in Func + extension methods + lambdas

I've been trying to make my way through this article: http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx ... And something on page 1 made me uncomfortable. In particular, I was trying to wrap my head around the Compose<>()…
Hobbes
  • 21
  • 2
2
votes
3 answers

Can you have an extension method on a type instead of on an instance of a type?

I can have an extension method like this: DateTime d = new DateTime(); d = d.GetRandomDate(); GetRandomDate is my extension method. However the above doesn't make much sense. What would be better is: DateTime d = DateTime.GetRandomDate(); However,…
NibblyPig
  • 51,118
  • 72
  • 200
  • 356
2
votes
1 answer

ControlCollection extension method optimization

got question regarding an extension method that I have written that looks like this: public static IEnumerable FindControlsOfType(this ControlCollection instance) where T : class { T control; foreach (Control ctrl in instance) { …
Johan Leino
  • 3,473
  • 1
  • 26
  • 27
2
votes
1 answer

Overloaded extension method is not being called

If we have a simple class like this one: class MyClass { public void DoSomething(object anObj) { Console.WriteLine("I am an Object: {0}", anObj); } public void DoSomething(string aStr) { Console.WriteLine("I am a…
osotorrio
  • 960
  • 1
  • 11
  • 30
2
votes
6 answers

C# extension method doesn't seem to exist

I can't seem to get the following extension method to be found in another class in the same namespace (MyProject.Util). using System.Collections.Specialized; namespace MyProject.Util { public static class Extensions { public static…
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
2
votes
1 answer

Any performance issue using static function Objects

I have a piece of code like this. I write this because I love extension methods and lambda expression: public static class TuneingRules { public static Func IsNodeHavingClearNone = (node) => { if (node.HasAttributes) …
sm.abdullah
  • 1,777
  • 1
  • 17
  • 34
2
votes
2 answers

NSubstitute throws CouldNotSetReturnDueToTypeMismatchException when mocking Query on NHibernate Session

I have a repository offering a GetAll method which again calls the Query extension method on the ISession instance of NHibernate. public ICollection GetAll() { return _session.Query().ToList(); } My unit test looks like…
2
votes
0 answers

How do I detect all extension methods for a type when the type may be generic?

I have reviewed some related questions and others. I'm trying to create an extension method for System.Type and a companion for System.Reflection.MethodBase to find all extension methods for a given type. I have the following: public static class…
Chris Lee
  • 51
  • 1
  • 5
2
votes
3 answers

How do I know if certain extensions methods 'mutate' the object or not?

Feel free to edit 'mutate' from the title if it's a poor choice of wording. My question is, relatively, simple. Take the following example: myCollection.OrderBy(o => o); How do I know whether OrderBy will/will not order myCollection or whether an…
user1017882
2
votes
0 answers

C# gives strange error during resolution of extension methods of generic type

I have 3 C# projects: LibraryA, LibraryB and LibraryC, such that LibraryB references LibraryA and LibraryC references LibraryB. Notice that LibraryC does not reference LibraryA. In LibraryA there is a single class: ClassA. In LibraryC there is a…
ironic
  • 8,368
  • 7
  • 35
  • 44
1 2 3
99
100