Best way to lookup alternative (overloads) for a method

I'm looking for guidance on the best way to lookup overloaded methods...

If I'm writing an analyser and I come across a method signature or a method used as part of an expression what is the best way to lookup any overloaded methods?

Should I use the R# PSI classes or should I use .Net reflection?



Comment actions Permalink

If you're in a plugin, you should use the PSI, since you're working with edit-time code, and not necessarily compilable code, so reflection won't help. The simplest way to get overloads is this:

  1. Find the closest IClassMemberDeclaration to the current PSI node - this is the declaration of the current property, method, etc
  2. Use memberDeclaration.GetContainingTypeDeclaration() to get at an instance of IClassLikeDeclaration (you might need to cast here)
  3. Get memberDelcaration.DeclaredElement and typeDeclaration.DeclaredElement
  4. As long as you have all of these, typeElement.EnumerateMembers(memberElement.ShortName, true) should give you all overloads. You might want to remove the current element with a simple check with Equals()

You can see this in action in CSharpOverloadsContextSearch, which is what the Navigate To -> Member Overloads menu uses.

Comment actions Permalink

Matt - thanks for the detailed answer




Please sign in to leave a comment.