Back to Devexpress

Promote to Generic Parameter

coderushforroslyn-401619-refactoring-assistance-promote-to-generic-parameter.md

latest2.9 KB
Original Source

Promote to Generic Parameter

  • Aug 19, 2021
  • 3 minutes to read

Purpose

Takes a class reference or struct reference inside a method block and turns that into a type parameter of the generic method. The Promote to Generic Parameter refactoring also updates all calling code.

This refactoring reduces the time it takes to make a method applicable to more classes in your code.

Availability

Available when the cursor is in a class reference or struct reference in a method.

How to Use

  1. Place the caret in a class reference or struct reference in a method.

  2. Press Ctrl + . or Ctrl + ~ to invoke the Code Actions Menu.

  3. Select Promote to Generic Parameter from the menu and press Enter.

After execution, this refactoring turns a regular method into a generic method.

csharp
using System;

namespace ConsoleApp15
{
    public class Verifier
    {
        public string CheckText(string text)
        {
            var validatedText = text ?? "";
            if (TrimSpaces)
                validatedText = validatedText.Trim();
            return validatedText;
        }
        public bool TrimSpaces { get; set; }
    }

    interface SpellChecker
    {
        string Check<T>(T verifier, string text) where T : Verifier;
    }

    class Control : SpellChecker
    {
        void SetRules<V>(V verifier)
            where V : Verifier
        {
            verifier.TrimSpaces = true;
        }
        public string Check<T>(T verifier, string text) where T : Verifier
        {
            SetRules<T>(verifier);
            return verifier.CheckText(text);
        }
        public void LogText(string text)
        {
            string verifiedText = Check<Verifier>(new Verifier(), text);
            Console.WriteLine(verifiedText);
        }
    }
}
vb
Namespace ConsoleApp15
    Public Class Verifier
        Public Function CheckText(ByVal text As String) As String
            Dim validatedText = If(text, "")
            If TrimSpaces Then validatedText = validatedText.Trim()
            Return validatedText
        End Function

        Public Property TrimSpaces As Boolean
    End Class

    Interface SpellChecker
        Function Check(Of T As Verifier)(ByVal verifier As T, ByVal text As String) As String
    End Interface

    Class Control
        Implements SpellChecker

        Private Sub SetRules(Of V As Verifier)(ByVal verifier As V)
            verifier.TrimSpaces = True
        End Sub

        Private Function Check(Of T As Verifier)(verifier As T, text As String) As String Implements SpellChecker.Check
            SetRules(Of T)(verifier)
            Return verifier.CheckText(text)
        End Function

        Public Sub LogText(ByVal text As String)
            Dim verifiedText As String = Check(Of Verifier)(New Verifier(), text)
            Console.WriteLine(verifiedText)
        End Sub
    End Class
End Namespace