[FxCop.设计规则]10. 类型应该被声明在命名空间中

10.     类型应该被声明在命名空间中

翻译概述:

命名空间的概念在C++中已经存在,但是对于大多数C++程序员来说,命名空间却很少被用到,C++程序员更喜欢在一组相关的类型前面添加相同的缩写,当项目变得越来越大时,这种传统的处理方式就很难适应软件系统规模。因此,在新的面向对象开发语言中无一例外的强化了命名空间的概念。

毋庸置疑,将类型放在全局命名空间中是一个很不好的设计。FxCop设计规则的第十条就对这条规则进行了校验。

原文引用:

Declare types in namespaces<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

TypeName:

DeclareTypesInNamespaces

CheckId:

CA1050

Category:

Microsoft.Design

Message Level:

Error

Certainty:

95%

Breaking Change:

Breaking


Cause: A public or protected type is defined outside the scope of a named namespace.

Rule Description

Types are declared within namespaces to prevent name collisions, and as a way of organizing related types in an object hierarchy. Types outside any named namespace are in a global namespace that cannot be referenced in code.

How to Fix Violations

To fix a violation of this rule, place the type in a namespace.

When to Exclude Messages

While it is never necessary to exclude a message from this rule, it is safe to do this when the assembly will never be used with other assemblies.

Example Code

The following example shows a library with a type incorrectly declared outside a namespace, and a type with the same name declared in a namespace.

[C#]

None.gif using  System;
None.gif 
None.gif
//  Violates rule: DeclareTypesInNamespaces.
None.gif
public   class  Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
return "Test does not live in a namespace!";
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif 
None.gif
namespace  GoodSpace
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
public override string ToString()
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
return "Test lives in a namespace!";
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}
   

 

The following application uses the library defined previously. Note that the type declared outside a namespace is created when the name Test is not qualified by a namespace. Note also that to access the Test type in Goodspace, the namespace name is required.

[C#]

None.gif using  System;
None.gif
using  GoodSpace;
None.gif 
None.gif
namespace  ApplicationTester
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public class MainHolder
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         Test t1 
= new Test();
InBlock.gif 
InBlock.gif         Console.WriteLine (t1.ToString());
InBlock.gif 
InBlock.gif         GoodSpace.Test t2 
= new GoodSpace.Test();
InBlock.gif 
InBlock.gif         Console.WriteLine (t2.ToString());
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}


This example produces the following output.

Test does not live in a namespace!

Test lives in a namespace!

 

引起的原因:

一个公共的或保护的类型没有定义在命名的命名空间中(非全局命名空间)

描述:

将类型定义在命名空间中,可以避免命名冲突,同时,提供了一种有效的方式组织一个对象层次hierarchy?翻译正确吗?)中的相关类型。一个不再任何命名的命名空间中的类型放在全局命名空间中,这样将不能被其他代码所引用(原文翻译过来如此,但是译者尝试后发现可以使用,并且不需要引用任何命名空间。)。

修复:

将这个类型放到一个命名的命名空间中。

例外:

尽量不要忽略这条校验,但是只要这个程序集不会被其他程序集使用,违反这条规则并不影响代码的安全性。

例程:

原文的例子演示了一个库中包含两个同名类型,一个在全局命名空间中,另一个在命名的命名空间中。在第二段代码中演示了如何调用它们。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值