다음을 통해 공유


적합한 제네릭을 사용하십시오.

업데이트: 2007년 11월

TypeName

UseGenericsWhereAppropriate

CheckId

CA1007

범주

Microsoft.Design

변경 수준

주요 변경

원인

외부에서 볼 수 있는 메서드에 System.Object 형식의 참조 매개 변수가 포함되어 있으며 포함하는 어셈블리가 .NET Framework 2.0을 대상으로 합니다.

규칙 설명

참조 매개 변수는 ref(Visual Basic의 경우 ByRef) 키워드로 수정되는 매개 변수입니다. 참조 매개 변수에 제공되는 인수 형식은 참조 매개 변수 형식과 정확하게 일치해야 합니다. 참조 매개 변수 형식에서 파생된 형식을 사용하려면 먼저 형식을 캐스팅하고 참조 매개 변수 형식의 변수에 할당해야 합니다. 제네릭 메서드를 사용하면 제약 조건에 따라 형식을 참조 매개 변수 형식으로 먼저 캐스팅하지 않고도 모든 형식을 메서드에 전달할 수 있습니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 메서드를 제네릭으로 만들고 Object 매개 변수를 형식 매개 변수로 바꿉니다.

경고를 표시하지 않는 경우

이 규칙에서는 경고를 표시해야 합니다.

예제

다음 예제에서는 제네릭이 아닌 메서드와 제네릭 메서드 모두로 구현되는 범용 스왑 루틴을 보여 줍니다. 제네릭 메서드를 사용할 경우 제네릭이 아닌 메서드에 비해 문자열을 효과적으로 바꿀 수 있음을 알 수 있습니다.

Imports System

Namespace DesignLibrary

   Public NotInheritable Class ReferenceParameters

      Private Sub New()
      End Sub

      ' This method violates the rule.
      Public Shared Sub Swap( _  
         ByRef object1 As Object, ByRef object2 As Object)

         Dim temp As Object = object1
         object1 = object2
         object2 = temp

      End Sub

      ' This method satifies the rule.
      Public Shared Sub GenericSwap(Of T)( _ 
         ByRef reference1 As T, ByRef reference2 As T)

         Dim temp As T = reference1
         reference1 = reference2
         reference2 = temp

      End Sub

   End Class

   Class Test

      Shared Sub Main()

         Dim string1 As String = "Swap"
         Dim string2 As String = "It"

         Dim object1 As Object = DirectCast(string1, Object)
         Dim object2 As Object = DirectCast(string2, Object)
         ReferenceParameters.Swap(object1, object2)
         string1 = DirectCast(object1, String)
         string2 = DirectCast(object2, String)
         Console.WriteLine("{0} {1}", string1, string2)

         ReferenceParameters.GenericSwap(string1, string2)
         Console.WriteLine("{0} {1}", string1, string2)

      End Sub

   End Class

End Namespace
using System;

namespace DesignLibrary
{
   public sealed class ReferenceParameters
   {
      private ReferenceParameters(){}

      // This method violates the rule.
      public static void Swap(ref object object1, ref object object2)
      {
         object temp = object1;
         object1 = object2;
         object2 = temp;
      }

      // This method satifies the rule.
      public static void GenericSwap<T>(ref T reference1, ref T reference2)
      {
         T temp = reference1;
         reference1 = reference2;
         reference2 = temp;
      }
   }

   class Test
   {
      static void Main()
      {
         string string1 = "Swap";
         string string2 = "It";

         object object1 = (object)string1;
         object object2 = (object)string2;
         ReferenceParameters.Swap(ref object1, ref object2);
         string1 = (string)object1;
         string2 = (string)object2;
         Console.WriteLine("{0} {1}", string1, string2);

         ReferenceParameters.GenericSwap(ref string1, ref string2);
         Console.WriteLine("{0} {1}", string1, string2);
      }
   }
}

관련 규칙

제네릭 형식에 매개 변수를 너무 많이 사용하지 마십시오.

컬렉션은 제네릭 인터페이스를 구현해야 합니다.

정적 멤버를 제네릭 형식으로 선언하지 마십시오.

제네릭 목록을 노출하지 마십시오.

멤버 시그니처에 제네릭 형식을 중첩하지 마십시오.

제네릭 메서드는 형식 매개 변수를 제공해야 합니다.

제네릭 이벤트 처리기 인스턴스를 사용하십시오.

참고 항목

참조

제네릭(C# 프로그래밍 가이드)