C #: discovery of extension methods

What tools or methods do you recommend for discovering C # extension methods in your code? They are in the correct namespace, but can be in any file in the solution.

In particular:

  • How to find all extension methods (and go to them) by current type in the code window?

I have Resharper (v4), so if I have a mechanism that I donโ€™t know about, please share!

+4
source share
4 answers

If you have a source, you can search for this Type identifier using regular expressions. Given the fact that it should be the first parameter of a function, something like this should do the trick:

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

At least in this way you can find out where extension methods are defined and add this namespace, and then rely on intellisense. Just ran this in a non-trivial project with many extension methods everywhere, and it worked. The only false positive was something like this:

 if(this is Blah... 

What we can fix by adding static to our search, since extension methods must be static:

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

This will not work for such cases:

 public static ExtensionMethod( this int iVal) { } 

But such a limitation of regular expressions. I am sure that certain several bodies can tell you all about the pain of using regular expressions to analyze the language.

Now, which, it seems to me, is missing in the IDE, it is possible to recognize extension methods that are in an unimported namespace. Similar to how you know the name of a class, if you type it, the IDE will give you a hint to either use it explicitly or import the namespace. In the end, this is how I import all my namespaces and often try to do the same with extension methods.

+5
source

This is pretty low-tech, but what about Ctrl-F to search for "this: b + MyClassName"?

+1
source

If you use VS, which I believe you are intellisense, will display all the available extension options for this object for you (marked in blue, added to the icon of a regular instance). This list may differ from file to file (a mthod called aMethod can mean two different things in two different files), although the type of object is the same (which is based on the method for determining extension methods)

0
source

If you have a resharper, just press and hold the ctrl key and select a method.

0
source

Source: https://habr.com/ru/post/1300277/


All Articles