当前位置: 首页 > news >正文

[FxCop.设计规则]8. 也许参数类型应该是基类型

8.     也许参数类型应该是基类型

翻译概述:

这条规则中,微软建议为了提高代码的可重用性,如果可能,尽量将参数类型修改为它的基类型。但是,这条规则并不是一条强制规则,在最后,微软提到,如果需要,使用子类型作为参数可以提高代码的健壮性。

关于这一点,译者比较趋向于尽量使用子类型作参数的类型,因为这样可以减少函数被滥用的情况(个人认为滥用比起重用的优先级更高一下)。而且,如果将来发现确实需要,我们可以通过简单的重构来达到提高重用性的目的。

原文引用:

Consider passing base types as parameters<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

TypeName:

ConsiderPassingBaseTypesAsParameters

CheckId:

CA1011

Category:

Microsoft.Design

Message Level:

Error

Certainty:

75%

Breaking Change:

Breaking


Cause: A method declaration includes a formal parameter that is a derived type, and the method only calls members of the parameter's base type.

Rule Description

When a base type is specified as a parameter in a method declaration, any type derived from the base type can be passed as the corresponding argument to the method. When the argument is used inside the method body, the specific method that is executed depends on the type of the argument. If the additional functionality provided by the derived type is not required, use of the base type allows the method to be more widely utilized.

How to Fix Violations

To fix a violation of this rule, change the type of the parameter to its base type.

When to Exclude Messages

It is safe to exclude a message from this rule if the method requires the specific functionality provided by the derived type or to enforce that only the derived type, or a more derived type, is passed to the method. In this case, the code will be more robust because of the strong type checking provided by the compiler and runtime.

Example Code

The following example shows a method, ManipulateFileStream, that can only be used with a FileStream object, which violates this rule. A second method, ManipulateAnyStream, satisfies the rule by replacing the FileStream parameter with a Stream.

[C#]

None.gif using  System;
None.gif
using  System.IO;
None.gif 
None.gif
namespace  DesignLibrary
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public class StreamUser
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
int anInteger;
InBlock.gif 
InBlock.gif      
public void ManipulateFileStream(FileStream stream)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
while((anInteger = stream.ReadByte()) != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            
// Do something.
ExpandedSubBlockEnd.gif
         }

ExpandedSubBlockEnd.gif      }

InBlock.gif 
InBlock.gif      
public void ManipulateAnyStream(Stream anyStream)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
while((anInteger = anyStream.ReadByte()) != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            
// Do something.
ExpandedSubBlockEnd.gif
         }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

InBlock.gif 
InBlock.gif   
class TestStreams
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         StreamUser someStreamUser 
= new StreamUser();
ExpandedSubBlockStart.gifContractedSubBlock.gif         MemoryStream testMemoryStream 
= new MemoryStream(new byte[] dot.gif{});
InBlock.gif         
using(FileStream testFileStream = 
InBlock.gif                  
new FileStream("test.dat", FileMode.OpenOrCreate))
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            
// Cannot be used with testMemoryStream.
InBlock.gif
            someStreamUser.ManipulateFileStream(testFileStream);
InBlock.gif 
InBlock.gif            someStreamUser.ManipulateAnyStream(testFileStream);
InBlock.gif            someStreamUser.ManipulateAnyStream(testMemoryStream);
ExpandedSubBlockEnd.gif         }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif


Related Rules

Members should not expose certain concrete types

 

引起的原因:

一个方法包含一个子类型的形参,但是方法内部仅仅使用了基类型中的成员。

描述:

如果一个方法的参数使用基类型,可以使用任何一个继承自这个基类型的类型作为参数调用这个函数。在一个方法内部使用一个类型,表示这个方法的执行依赖于这个参数的类型。如果不需要使用子类型所扩展的功能,使用基类型使方法可以被更广泛的使用。

修复:

修改参数类型为它的基类型。

例外:

如果这个方法需要扩展的功能,或需要强制只有指定的一个或多个子类型可以使用这个方法,这条规则可以忽略。这种情况下,代码还会更加的健壮。因为编译和运行时进行了强的类型检查。

转载于:https://www.cnblogs.com/Cajon/archive/2005/06/07/169761.html

相关文章:

  • [XA]我们为什么不用XP(eXtreme Programming)极限编程?
  • Windows IIS 6安全保护贴—URL授权全攻略!
  • 身为男人应该做的10件事(转载)
  • 今天下午开了几个小时的会,没有一句话与我有关的,搞到我直打瞌睡。
  • 绘制概念图的工具
  • ArcGIS Explorer,ESRI的...?
  • Vs2005终于出来了,搞了一个英文版的下载地址。
  • 人的惰性
  • MOF 思考
  • 自己真是落伍呀,腾讯收购Foxmail都不知道
  • 发布biztalk的一些onenote笔记,是一个mht格式的,希望对大家有帮助
  • 如何保持Oracle数据库的优良性能
  • Flying,毕业设计
  • (转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据
  • 解读C#中的正则表达式
  • Docker: 容器互访的三种方式
  • E-HPC支持多队列管理和自动伸缩
  • github指令
  • HomeBrew常规使用教程
  • javascript从右向左截取指定位数字符的3种方法
  • Java新版本的开发已正式进入轨道,版本号18.3
  • JSDuck 与 AngularJS 融合技巧
  • MaxCompute访问TableStore(OTS) 数据
  • react-native 安卓真机环境搭建
  • 编写高质量JavaScript代码之并发
  • 从PHP迁移至Golang - 基础篇
  • 聊聊hikari连接池的leakDetectionThreshold
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 数据仓库的几种建模方法
  • 在Docker Swarm上部署Apache Storm:第1部分
  • - 转 Ext2.0 form使用实例
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • ​MySQL主从复制一致性检测
  • #QT项目实战(天气预报)
  • (1)虚拟机的安装与使用,linux系统安装
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • *++p:p先自+,然后*p,最终为3 ++*p:先*p,即arr[0]=1,然后再++,最终为2 *p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1]
  • .[hudsonL@cock.li].mkp勒索病毒数据怎么处理|数据解密恢复
  • .NET CORE 第一节 创建基本的 asp.net core
  • .NET Framework 3.5中序列化成JSON数据及JSON数据的反序列化,以及jQuery的调用JSON
  • .net Signalr 使用笔记
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装
  • .net连接oracle数据库
  • .NET是什么
  • .skip() 和 .only() 的使用
  • @JsonFormat与@DateTimeFormat注解的使用
  • @TableId注解详细介绍 mybaits 实体类主键注解
  • @取消转义
  • [⑧ADRV902x]: Digital Pre-Distortion (DPD)学习笔记
  • [Angular] 笔记 20:NgContent