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

ASP.NET
关于如何操作其他窗体的控件或变量的方法
一个简单的.net remoting客户端例子
System.Runtime.Remoting.Activation.ActivationServices.CreateInstance()
单点登录—演示:passport.winspace.net
WebSerivce研究笔记
[新]可精确到1毫秒的用以在记录文件中写入自定义的调试信息(主要是时间)的组件
vs.net 2005中文版下载地址收藏
VS2005Beta2安装几点经验和体会
一个Web文件上传的C#源代码
C#重点知识详解(一)
利用C#线程机制实现应用程序的单实例运行
C#重点知识详解(二)
c#重点知识详解(三)
c#重点知识详解(四)
c#重点知识解答(五)
c#重点知识详解(六)
通过命令行方式使用NUnit进行UT
程序控制开始菜单的弹出 C#
XSL、XML教程-DVBBS皮肤制作 最基本的语法
MapX从数据库读取数据形成新图层(C#)

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


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