代码改变世界

[FxCop.设计规则]7. 集合类应该实现泛型接口

2005-05-29 17:16  Colin Han  阅读(1881)  评论(0编辑  收藏  举报

7.     集合类应该实现泛型接口

翻译概述:

这条规则中提到了.NET framework 2.0中新引入的技术“范型”,.NET Framework提供了很多使用范型实现的类,其中最主要的就是关于集合的类型。使用这些类型,我们不再需要定义太多的强制类型安全的集合类型。

但是往往我们为了满足独特的需求,必须建立一些自定义的集合类型。如何让他们.NET Framework中提供的范型集合类协调的工作,如何让我们的集合类型能够有更加灵活的可用性。在这条规则中,微软给出了一个基本原则——集合类应该实现范型集合接口。

原文引用:

Collections should implement generic interface

TypeName:

CollectionsShouldImplementGenericInterface

CheckId:

CA1010

Category:

Microsoft.Design

Message Level:

Error

Certainty:

90%

Breaking Change:

NonBreaking


Cause: An externally visible type implements the System.Collections.IEnumerable interface but does not implement the System.Collections.Generic.IEnumerable<T> interface, and the containing assembly targets .NET Framework version 2.0. This rule ignores types that implement System.Collections.IDictionary.

Rule Description

To broaden the usability of a collection, implement one of the generic collection interfaces. Then the collection can be used to populate generic collection types such as the following:

How to Fix Violations

To fix a violation of this rule, implement one of the following generic collection interfaces:

When to Exclude Messages

It is safe to exclude a message from this rule; however, the collection will have a more limited use.

Example Code

The following example shows a collection, IntegerCollection, that violates the rule. In GenericIntegerCollection, the collection is modified to satisfy the rule by implementing System.Collections.Generic.IEnumerable<T>. Finally, the collection is used to populate a strongly typed stack.

[C#]

using System;
using System.Collections;
using System.Collections.Generic;

namespace DesignLibrary
{
   
// This class violates the rule.
   public class IntegerCollection: CollectionBase
   
{
      
// Methods overrides using Int32.
   }

 
   
// This class satisfies the rule.
   public class GenericIntegerCollection: 
      CollectionBase, IEnumerable
<int>
   
{
      
public int Add(int value)
      
{
         
return InnerList.Add(value);
      }

 
      
// Other method overrides using Int32.
 
      
public new IEnumerator<int> GetEnumerator()
      
{
         
foreach (int data in InnerList)
         
{
            yield 
return data;
         }

      }

   }

 
   
class Test
   
{
      
static void Main()
      
{
         IntegerCollection intCollection 
= new IntegerCollection();
 
         
// The following line would generate a compiler error.
         
//Stack<int> integerStack = new Stack<int>(intCollection);
 
         GenericIntegerCollection genericIntCollection 
=
            
new GenericIntegerCollection();
         genericIntCollection.Add(
2);
         genericIntCollection.Add(
4);
 
         Stack
<int> integerStack = new Stack<int>(genericIntCollection);
         Console.WriteLine(integerStack.Pop());
         Console.WriteLine(integerStack.Pop());
      }

   }

}


Related Rules

Avoid excessive parameters on generic types

Do not declare static members on generic types

Do not expose generic lists

Do not nest generic types in member signatures

Generic methods should provide type parameter

Use generic event handler instances

Use generics where appropriate

See Also

Generics DesignGuidelines

引起的原因:

一个使用.NET Framework2.0的程序集有一个输出类型实现了System.Collections.IEnumerable接口,但是没有实现System.Collections.Genceric.IEnumerable<T>接口。如果这个类型实现了System.Collections.IDictionary接口,将不被这个规则所检察。

描述:

为了提升一个集合的可用性,应该实现范型集合接口中的一个。这样,这个集合将能够被用来声明下面的范型类。

修复:

实现下面的范型集合接口中的一个。

例外:

不符合这条规则是安全的,但是,这样将会使你的集合类有更多的使用限制。

译注:

实现至少一个泛型集合接口使你的类型能够被用来构造下面的范型集合类。

例如在原文的例子中的GenericIntegerCollection对象就可以被用来构造一个Stack<int>对象,但是如果你不实现这些接口,构造Stack<int>对象将会产生一个编译警告。实现范型集合接口将会使你的使用者拥有更多的灵活性去使用你的集合类。