Share via


CA1007:在适用处使用泛型

类型名

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);
      }
   }
}

相关规则

CA1005:避免泛型类型的参数过多

CA1010:集合应实现泛型接口

CA1000:不要在泛型类型中声明静态成员

CA1002:不要公开泛型列表

CA1006:不要将泛型类型嵌套在成员签名中

CA1004:泛型方法应提供类型参数

CA1003:使用泛型事件处理程序实例

请参见

参考

泛型(C# 编程指南)