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

ASP.NET
关于数据绑定在Web页面呈现效果的一点小技巧
菜鸟模仿duwamish开发时常见的错误
基于.net的快速开发思想
ASP.Net实现将Word转换PDF格式
用ASP.NET建立一个在线RSS新闻聚合器
ASP.NET图象处理详解
名称地址(Namespace)
Win中Net命令的另类用法
浅析.Net下的多线程编程
构造.NET环境下的网页下载器 (1)
构造.NET环境下的网页下载器 (2)
ASP.NET编程中的十大技巧(上)
ASP.NET编程中的十大技巧(下)
C# 3.0语言详解之基本的语言增强
Windows.NET Server: XML Web 服务
ASP.Net中程序构架与程序代码的分离
.NET 数据访问架构指南
用Visual C#打造多页面网页浏览器
.NET 2.0 基础类库中的范型:范型集合
.NETCompactFramework的使用技巧

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 92 ::
收藏到网摘: 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.
}
}
}
引起的原因:一个公共抽象类型拥有一个公共的构造函数描述:构造函数被用来建立一个对象实例,但是你不能建立一个抽象类型的实例,抽象类型的构造函数就仅仅能够被它的继承类型使用。因此,为一个抽象类构造公共构造函数是一个错误的设计。修复:如果需要修复这个问题,可以声明这个构造函数为保护型,或者,声明这个类型不是一个抽象类型。