当前位置: 首页 > 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#中的正则表达式
  • 【刷算法】求1+2+3+...+n
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • JS基础篇--通过JS生成由字母与数字组合的随机字符串
  • mysql innodb 索引使用指南
  • SegmentFault 2015 Top Rank
  • Spring Cloud Feign的两种使用姿势
  • spring学习第二天
  • vue:响应原理
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • vue的全局变量和全局拦截请求器
  • 闭包,sync使用细节
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 从PHP迁移至Golang - 基础篇
  • 高性能JavaScript阅读简记(三)
  • 关于 Cirru Editor 存储格式
  • 力扣(LeetCode)22
  • 那些年我们用过的显示性能指标
  • 前端攻城师
  • 深度学习中的信息论知识详解
  • 微信开放平台全网发布【失败】的几点排查方法
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • 通过调用文摘列表API获取文摘
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • ​【已解决】npm install​卡主不动的情况
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (分布式缓存)Redis分片集群
  • (一)UDP基本编程步骤
  • (一)基于IDEA的JAVA基础1
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • ***原理与防范
  • .gitignore文件—git忽略文件
  • .net Signalr 使用笔记
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • .sh文件怎么运行_创建优化的Go镜像文件以及踩过的坑
  • //解决validator验证插件多个name相同只验证第一的问题
  • @Autowired标签与 @Resource标签 的区别