How I can find all extension methods in solution ?
5 Answers
If I were doing it I would search all files for the string "( this " -- your search string may differ based on your formatting options.
EDIT: After a little bit of experimentation, the following seems to work for me with high precision using "Find in Files" (Ctrl-Shift-F)
- Search string: "
\( this [A-Za-z]
" (minus quotes, of course) - Match case: unchecked
- Match whole word: unchecked
- Use: Regular Expressions
- Look at these file types: "*.cs"

- 524,688
- 99
- 697
- 795
-
"this" is often passed in as an argument - I guess you'd need to match the "(this type name" pattern, followed by "," or ")", and with any amount of whitespace in the middle. Not a simple regex (I didn't even try...) – Marc Gravell May 13 '09 at 12:58
-
Yes, but in my formatting it would be "( this, ", not "( this " so it wouldn't match. – tvanfosson May 13 '09 at 14:30
-
...except when it's the only argument, so perhaps a regex with "( this [A-Za-z]", though I just ran it on one of my solutions and only found 3 examples of it passed as an argument and 50 or so extension methods (lots of HtmlHelper extensions for MVC). – tvanfosson May 13 '09 at 14:32
-
is it always a space infront of 'this'? – Svish May 14 '09 at 06:25
-
It depends on your formatting settings. It always is for me. – tvanfosson May 14 '09 at 10:08
-
As Search String you must use `\(\s*this\s*IEnumerator` (in case search extension for "IEnumerator"), because between {"(" and "this"} and {"this" and "
"} can exist any number of whiteSpaces – Denis Sivtsov Jun 26 '23 at 18:44
I'd look at the generated assemblies using reflection; iterate through the static types looking for methods with [ExtensionAttribute]
...
static void ShowExtensionMethods(Assembly assembly)
{
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass && !type.IsGenericTypeDefinition
&& type.BaseType == typeof(object)
&& type.GetConstructors().Length == 0)
{
foreach (MethodInfo method in type.GetMethods(
BindingFlags.Static |
BindingFlags.Public | BindingFlags.NonPublic))
{
ParameterInfo[] args;
if ((args = method.GetParameters()).Length > 0 &&
HasAttribute(method,
"System.Runtime.CompilerServices.ExtensionAttribute"))
{
Console.WriteLine(type.FullName + "." + method.Name);
Console.WriteLine("\tthis " + args[0].ParameterType.Name
+ " " + args[0].Name);
for (int i = 1; i < args.Length; i++)
{
Console.WriteLine("\t" + args[i].ParameterType.Name
+ " " + args[i].Name);
}
}
}
}
}
}
static bool HasAttribute(MethodInfo method, string fullName)
{
foreach(Attribute attrib in method.GetCustomAttributes(false))
{
if (attrib.GetType().FullName == fullName) return true;
}
return false;
}

- 1,026,079
- 266
- 2,566
- 2,900
-
Just to add, it can be any attribute called ExtensionAttribute, it ned not be the same type as supplied in System.Code.dll. This allows you to use this feature while targeting .NET 2. – leppie May 13 '09 at 12:38
-
True; indeed, I've used several - in LINQBridge, and MiscUtil among others... I'll update it... – Marc Gravell May 13 '09 at 12:51
-
1It does not need that namespace either :) Any attribute named ExtensionAttribute will work. – leppie May 13 '09 at 13:56
-
OK, that last is news to me - but getting a bit into the exotic now ;-p I'll leave it "as is", but good info, cheers. – Marc Gravell May 13 '09 at 14:57
Maybe the code in this article about how to find extension methods targeting object could be used? Could for example be rewritten slightly and use it to dump all extension methods instead of just those targeting object
.

- 152,914
- 173
- 462
- 620
Do you just want to check the sourcecode (just look for (this ...
in the files) or your running programm by reflection (in this case, this this discussion can help you)?
-
Yep. Not to forget about whitespace and newlines between the bracket and `this`. – Dirk Vollmar May 13 '09 at 11:54
Solution wide text search with a regex matching your coding style. Something like "( *this +"
(added the first optional space to get some error tollerance).

- 59,031
- 16
- 99
- 143