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

ASP.NET
[EnterpriseServices]利用assembly定义我们的组件在COM+中的注册方式
crystal reports for Visual Studio .NET(webformsample)
XML指南:微软的XML解析器
展现C# 清单5.10 生成exe文件执行的问题
vb.net 读写xml方法(1)
选择文件夹的对话框控件c#
特洛伊木马服务器源代码(C#)
上传图片画带阴影的水印.(C#)
vb.net读写xml(2)--实现datagrid与xml的沟通(原创)
列出所有的sheet,然后点击其中的一个,自动跳过去
从VB中的Datagride中向excel导入数据
XML指南:XML CDATA
XML指南:XML编码
XMLDOM对象方法:Document对象方法
XMLDOM对象方法:对象事件
XMLDOM对象方法:对象属性
[一个登录窗体的完整范例,包括登录,密码更改,输入错误三次退出]
[关于判断输入数据是否在数据库中的方法。]
[VBA]office中内建的faceid所对应的图标
web组件设计,利用接口(IPostBackDataHandler)产生数据回传的问题

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


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