[FxCop.设计规则] 3. 避免定义空的接口

 

版权声明:

 

本文由timewolf完成,首发于CSDN ,作者保留版权。
未经许可,不得使用于任何商业用途。
欢迎转载,但请保持文章及版权声明完整。
如需联络请发邮件:karla9(AT)eyou(dot)com

3.     避免定义空的接口

原文引用:

Avoid empty interfaces

TypeName:

AvoidEmptyInterfaces

CheckId:

CA1040

Category:

Microsoft.Design

Message Level:

Warning

Certainty:

90%

Breaking Change:

Breaking


Cause: The interface does not declare any members or implement other interfaces.

Rule Description

Interfaces define members that provide a behavior or usage contract. The functionality described by the interface can be adopted by any type, regardless of where the type appears in the inheritance hierarchy. A type implements an interface by providing implementations for the interface's members. An empty interface does not define any members, and as such, does not define a contract that can be implemented.

If your design includes empty interfaces that types are expected to implement, you are most likely using an interface as a marker, or a way of identifying a group of types. If this identification will occur at runtime, the correct way to accomplish this is to use a custom attribute. Use the presence or absence of the attribute, or the attribute's properties, to identify the target types. If the identification must occur at compile time, then using an empty interface is acceptable.

How to Fix Violations

Remove the interface or add members to it. If the empty interface is being used to label a set of types, replace the interface with a custom attribute.

When to Exclude Messages

It is safe to exclude a message from this rule if the interface is used to identify a set of types at compile-time.

Example Code

The following example shows an empty interface.

[C#]

using  System;

namespace  DesignLibrary
{
   
public interface BadInterface
   
{
      
// Violates rule: AvoidEmptyInterfaces.
   }

}

 

引发原因:

一个接口没有任何成员,且没有继承其他接口

规则描述:

接口定义了一组成员,用以描述接口拥有的行为或契约(usage contract)任何类型都可以实现该接口, 无需考虑该类的继承层次。一个类通过实现接口的成员从而实现接口。一个空的接口没有定义任何成员,因此,也就没有任何契约能够被实现。

如果你的设计包含空的接口,并希望它被某些类实现,你很可能是将该接口作为一个标记,用以标示一组类型。如果只需在运行时区分这些类型,你可以使用自定义特性(attribute)。通过检查某个类是否包含该特性,以及特性所包含的属性(property),来达到识别某个类的目的;当然,如果你希望在编译期就执行此种检查,就只好使用空接口了。

修复:

删除该接口,或添加必要的成员到该接口。

如果该接口被用于运行时的类型识别,将接口替换为自定义特性。

译注:

自定义特性是.NET新引入的一种开发元素,通过将自定义特性和应用程序元数据保存在一起,开发人员可以获得类似自定义关键字的能力。以下是MSDN中对于特性的描述:

如果您使用过 C++,您或许对包含关键字(如 public private)的声明比较熟悉。这些关键字提供有关类成员的附加信息,还通过描述类成员对其他类的可访问性来进一步定义类成员的行为。由于编译器被显式设计为识别预定义关键字,因此传统上您没有机会创建自己的关键字。但是,公共语言运行库允许您添加类似关键字的描述性声明(称为特性)来批注编程元素,如类型、字段、方法和属性。

为运行库编译代码时,该代码被转换为 Microsoft 中间语言 (MSIL),并同编译器生成的元数据一起被放到可移植可执行 (PE) 文件的内部。特性使您得以向元数据中放置额外的描述性信息,并可使用运行库反射服务提取该信息。当您声明从 System.Attribute 派生的特殊类的实例时,编译器创建特性。

.NET Framework 出于多种原因使用特性并通过它们解决若干问题。特性可以描述如何序列化数据,指定用于强制安全性的特性,以及限制实时 (JIT) 编译器的优化以使代码易于调试。特性还可以记录文件名或代码作者,或在窗体开发阶段控制控件和成员的可见性。

可使用特性以几乎所有可能的方式描述代码,并以富有创造性的新方式影响运行库行为。特性允许您向 C#C++ 托管扩展、Microsoft Visual Basic .NET 或其他任何以运行库为目标的语言添加自己的描述性元素,而不必重新编写编译器。

使用特性可以获得很大的开发灵活性,并且可以提供很多非常清晰易读的代码,笔者看到的在NUnit中的一段代码可以作为使用特性的很好例子:
上面的例子中,基类里通过自定义特性分析命令行参数,并将他们分别填充到字段中。例如用户输入下面的命令行参数:

namespace  NUnit.Util
{
     
using System;
     
using Codeblast;

     
public class ConsoleOptions : CommandLineOptions
     
{
         [Option(Description 
= "Fixture to test")]
         
public string fixture;

         [Option(Description 
= "Project configuration to load")]
         
public string config;
 
         [Option(Description 
= "Name of XML output file")]
         
public string xml;
 
         [Option(Description 
= "Name of transform file")]
         
public string transform;
 
         [Option(Description 
= "Display XML to the console")]
         
public bool xmlConsole;
     }

}


NUnit-Console.exe /config:a.cfg

基类就会将“a.cfg”填充到config变量中。

相关的代码可以参考NUnit/nunit.util.dll/CommandLineOption.cs NUnit/nunit.util.dll/ConsoleOption.cs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值