Today I'm going to discuss Extension Methods.
Note: Information, features and ideas contained in this post are preliminary and subject to change in the release version of ReSharper with C# 3.0 support.
In short, extension methods feature provides ability to pseudo-extend some type's public interface via static member. Compiler looks for static methods marked with System.Runtime.CompilerServices.ExtensionAttribute in the static class with the same attribute. It then converts obj.ExtensionMethod(params) into ExtensionClass.ExtensionMethod(obj, params) while compiling. There is also syntactic sugar to mark methods as extensions - you prefix first parameter of ExtensionMethod with "this" keyword:
public static class ListExtensions
{
public static void Process(this List<int> list, int p) {}
}
As soon as you have instance of List
void Foo(List<int> list)
{
list.Process(1);
}
What does it mean for ReSharper? Well, besides parsing and resolving, it mostly means updating many features to support extension methods, like parameter information, navigation and search.
There will be a number of context actions, analyses and quick fixes to help with extension methods. For example, you already added "this" to the first parameter of the static member, but still using it in the form of a static method in your code, like this: ListExtensions.Process(list,1). In this case suggestion could be issued to convert to the pseudo-instance form: list.Process(1);
There are also some things that should be done specifically for Extension Methods feature. It would be very handy to have completion feature which lists non-imported extensions and insert using directives when you select one, much like Type Name Completion. Most likely we will extend existing feature to work after "dot" and rename it as "Import Name Completion".
It would be nice to have a refactoring to convert existing static member in a static class into extension method and update all usages to the pseudo-instance form.
When implementing interfaces, ReSharper could also look for extension methods that match interface members and current type, and suggest to use them as default implementation instead of throwing NotImplementedException.
If you have any other ideas about supporting Extension Methods in ReSharper, you are welcome to comment on this post.