This is the first of possibly a series of useful queries that can be added to the Query Editor (available in PowerCommands for Reflector addin). I’m hoping that some of the queries that I have created and found useful end up being useful for you too.
Find Extension Methods of this Type
List all of the extension methods in all loaded assemblies, of a selected type in Reflector.
Motivation
Yesterday I some time working on the next version of my website – which is going to be written in Asp.Net MVC. One of the things I found myself looking for in Reflector was all of methods that are available off of the HtmlHelper class … however most of the methods that I was looking for are extension methods – so they are a little harder to find.
How It Works
Extension methods are implemented as methods with the ExtensionAttribute on them. The first parameter in the method’s signature is the type (class) the methods is extending. In order to create a query that will locate extension methods of a specific type we need to do 2 things:
- Find all extension methods
- Of those extension methods, find the ones where the first parameter has a specific type (one that matches the name of the selected type in this case).
This means the query needs to look through all of the methods for ones with the ExtensionAttribute and then filter that list to only the methods that have a first parameter of the type equal to the selected type.
Details
If you want to create this query using the Query Editor for yourself, you can use the following information to do so (don’t forget to add it to the menu by checking ‘Show on Menu’ in the Save dialog):
| Name | Find Extension Methods of this Type |
| Query Type | Type |
| Output Type | Linked List View |
| Query Text | |
from a in AssemblyManager.Assemblies.Cast<IAssembly>()
from m in a.Modules.Cast<IModule>()
from t in m.Types.Cast<ITypeDeclaration>()
from meth in t.Methods.Cast<IMethodDeclaration>()
from ca in meth.Attributes.Cast<ICustomAttribute>()
where ((ITypeReference)ca.Constructor.DeclaringType).Name == "ExtensionAttribute"
&& ((IMethodSignature)meth).Parameters.Count > 0
&& ((IMethodSignature)meth).Parameters[0].ParameterType.ToString() == ActiveItem.Name
select meth
If you add the above query with the provided information, you should now get a new query added to the Run Query item on the Type Declaration context menu.
After building this query, it turns out that the majority of the HtmlHelper extensions are under the System.Web.Mvc.Html namespace. So far the query has been useful for me, hopefully it will be for you too.