views:

164

answers:

5

How I can find all extension methods in solution ?

+4  A: 

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"
tvanfosson
"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
Yes, but in my formatting it would be "( this, ", not "( this " so it wouldn't match.
tvanfosson
...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
is it always a space infront of 'this'?
Svish
It depends on your formatting settings. It always is for me.
tvanfosson
+1  A: 

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)?

Dario
Yep. Not to forget about whitespace and newlines between the bracket and `this`.
0xA3
+2  A: 

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.

Svish
+2  A: 

Solution wide text search with a regex matching your coding style. Something like "( *this +" (added the first optional space to get some error tollerance).

Daniel Brückner
+4  A: 

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;
}
Marc Gravell
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
True; indeed, I've used several - in LINQBridge, and MiscUtil among others... I'll update it...
Marc Gravell
It does not need that namespace either :) Any attribute named ExtensionAttribute will work.
leppie
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
It surprised me too :)
leppie