当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > [FxCop.设计规则]1. 抽象类不应该拥有构造函数

ASP.NET
asp.net图片加水印
Asp.Net中页面运行时动态载入的UserControl内元素的事
ASP.NET底层架构探索之再谈.NET运行时(二)
借助封装类实现线程调用带参方法
面向对象设计思想(C#)
asp.net URL重写(URLRewriter) 简化版
GUID在.net里的使用,就用System.Guid结构
不要忽略c#中的using和as操作符
C#中ref和out的使用小结
C#的Web XML编程
asp.net2.0下 如何实现服务器端压缩包自解压
javascript如何调用C#后台代码中的过程 和ASP.NET调用
在ASP.NET中自动给URL加上超链接
ASP.NET 中处理页面“回退”的方法
ASP.NET的四种错误机制
asp.net跳转页面的三种方法比较
ASP.NET2.0中将GridView导出到Excel文件中
ASP.NET 2.0中GridView无限层复杂表头的实现
ASP.NET 2.0 中动态添加 GridView 模板列
十天学会ASP.net之第一天

ASP.NET 中的 [FxCop.设计规则]1. 抽象类不应该拥有构造函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 45 ::
收藏到网摘: n/a

1. 抽象类不应该拥有构造函数原文引用:Abstract types should not have constructors
TypeName:
AbstractTypesShouldNotHaveConstructors
CheckId:
CA1012
Category:
Microsoft.Design
Message Level:
CriticalWarning
Certainty:
95%
Breaking Change:
NonBreaking
Cause: A public type is abstract and has a public constructor.
Rule Description
Constructors on abstract types can only be called by derived types. Because public constructors create instances of a type, and you cannot create instances of an abstract type, an abstract type with a public constructor is incorrectly designed.
How to Fix Violations
To fix a violation of this rule, either make the constructor protected, or do not declare the type as abstract.
When to Exclude Messages
Do not exclude a message from this rule.
Example Code
The following example contains an abstract type that violates this rule, and an abstract type that is correctly implemented.

[C#]
using System;
namespace DesignLibrary
{
public abstract class BadAbstractClassWithConstructor
{
// Violates rule: AbstractTypesShouldNotHaveConstructors.
public BadAbstractClassWithConstructor()
{
// Add constructor logic here.
}
}
public abstract class GoodAbstractClassWithConstructor
{
protected GoodAbstractClassWithConstructor()
{
// Add constructor logic here.
}
}
}
引起的原因:一个公共抽象类型拥有一个公共的构造函数描述:构造函数被用来建立一个对象实例,但是你不能建立一个抽象类型的实例,抽象类型的构造函数就仅仅能够被它的继承类型使用。因此,为一个抽象类构造公共构造函数是一个错误的设计。修复:如果需要修复这个问题,可以声明这个构造函数为保护型,或者,声明这个类型不是一个抽象类型。