Make Extension

The refactoring creates a static Extensions class, and moves the current method to it, which makes the method an extension of the selected parameter type. The refactoring also changes method calls so that the method is called as a member of the element initially used as a parameter, and activates Linked Identifiers for the method calls.

You can use Target Picker to specify the Extensions class location in the code.

Availability

Available from the context menu or via shortcuts:

  • when the cursor is on a method that has at least one parameter.

Examples

class TestClass
{
    public string TestMethod(int i)
    {
        return String.Format("i = {0}", i);
    }
    public void MyMethod()
    {
        string str = TestMethod(45);
    }
}
Class TestClass
    Public Function TestMethod(ByVal i As Integer) As String
        Return String.Format("i = {0}", i)
    End Function
    Public Sub MyMethod()
        Dim str As String = TestMethod(45)
    End Sub
End Class

Result:

public static class Extensions
{
    public static string TestMethod(this int i)
    {
        return String.Format("i = {0}", i);
    }
}
class TestClass
{
    public void MyMethod()
    {
        string str = 45.TestMethod();
    }
}
Public Module Extensions
    <Extension()> _
    Public Function TestMethod(ByVal i As Integer) As String
        Return String.Format("i = {0}", i)
    End Function
End Module
Class TestClass
    Public Sub MyMethod()
        Dim str As String = Call 45.TestMethod()
    End Sub
End Class

Animation

rsMakeExtension