5

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 not aware of - please share!

Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220

5 Answers5

5

If you have the source then you can search for this Type identifier using regular expressions. Considering the fact that it has to be the first parameter to the function something like this should do the trick:

\(this:b+:i:b+:i

At least this way you can discover where the extensions methods are defined and add that namespace, then rely on intellisense. Just ran this on a non-trivial project with lots of extensions methods everywhere and it worked a treat. The only false positive was something like this:

if(this is Blah...

Which we can fix by adding static to our search since the extension methods have to be static:

static.*\(this:b+:i:b+:i

This won't work for cases like this:

public
  static ExtensionMethod(
   this int
    iVal) {
}

But that's kind of the limitation of regular expressions. I am sure certain somebodies can tell you all about the pain of using regular expressions to parse a language.

Now, what I think is missing from the IDE is the ability to recognise the extension methods that are in a non-imported namespace. Similar to when you know the classname, if you type it up, the IDE will give you a hint to either use it explicitly or import the namespace. After all, that's how I import all my namespaces and frequently find myself trying to do the same to extension methods.

Community
  • 1
  • 1
Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128
  • The static.*\(this:b+:i:b+:i is not working for me, I do not find anything in Visual Studio. I do not understand the : colon and the i in the expression. How schould I use this with Visual Studio search with Regular Expressions enabled. What dows work for my is searching with static.*\(this\sMyType – donttellya Jun 28 '22 at 10:33
1

This is pretty low-tech, but how about Ctrl-F searching for "this:b+MyClassName" ?

Jimmy
  • 89,068
  • 17
  • 119
  • 137
  • Unfortunately, it's often not that simple. For example, if you wanted to find *all* extension methods applicable to an `int[]` array then you'd probably need to search for `this object`, `this T`, `this T[]`, `this int[]`, `this ICloneable`, `this IList`, `this ICollection`, `this IEnumerable`, `this IList`, `this IList`, `this ICollection`, `this ICollection`, `this IEnumerable` and `this IEnumerable`. (And, of course, there's no guarantee that the generic type parameters on the extension methods will actually be named `T` rather than `TFoo` or `TBar` etc.) – LukeH Feb 04 '10 at 10:11
0

If you are using VS which I guess you are intellisense will show all the avialable extensionmethod for a given object for you (marked with a blue thingy added to the usual instance method icon). That list might differ from file to file (a mthod called aMethod might mean two different things in two different files) eventhough the object type is the same (which is based on the way extension methods are found)

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • As long as you have the namespace in which they are defined included in your class file. – ChrisF Feb 04 '10 at 10:21
  • @chris a requirement that OP states is met :) things however get very funny if you have methods with the same name in different namespaces and put both namespaces in the using list. Don't reorder your using list if you don't want to change the way your code works then – Rune FS Feb 04 '10 at 10:52
0

If you've got resharper, just hold down the ctrl key and click on the method.

Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57
0

If you have installed the ILSpy extension in Visual Studio (I am using 2022) then you can:

  • Right click on the class/type and select -> Open code in ILSpy
  • In ILSpy right click on the type and select -> Analyze
  • In the Analyze window you will see a node "Extension methods" (if any exists, else no node is shown)
donttellya
  • 452
  • 1
  • 3
  • 13