构造函数中的可重写方法-帮助修复 [英] Overridable Methods In Constructors -Help to Fix

查看:127
本文介绍了构造函数中的可重写方法-帮助修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#项目上使用fxCop作为C#编码标准的基础.

Im attempting to use fxCop on a C# Project as kind of basis for C# coding standards.

我正在使用的项目称为S#arp体系结构,可在此处免费使用: S#阿普拱门

The project Im using is called S#arp Architecture and is freely available here: S#Arp Arch

现在,如果我运行fxCop(大多数问题已经修复),我需要修复构造函数中可重写方法的CA2214 fxcop错误.

Now if I run fxCop (most things have been fixed already) I need to fix the CA2214 fxcop error for overridable methods in contructors.

此刻一些违反代码的代码如下:

At the moment a piece of violating code looks like this:

public class Region : Entity, IHasAssignedId<int>
{
    public Region(string description)
    {
        Check.Require(!string.IsNullOrEmpty(description));
        this.Description = description;
    }

    protected Region()
    {
    }

    [DomainSignature]
 >  public virtual string Description { get; protected set; }

    public virtual void SetAssignedIdTo(int assignedId)
    {
        Id = assignedId;
    }
}

大多数其他CD文件中都以这种方式调用它:

Most of it is called in this way in the other cd files:

public static RegionDto Create(Region region)
{
    if (region == null)
    {
        return null;
    }

    return new RegionDto()
    {
        Id = region.Id,
    >   Description = region.Description
    };
}

我尝试过更改方法的类型(私有/受保护等),但是fxCop和单元测试经常有冲突的需求,fxcop说它不喜欢虚拟方法的构造函数,但是单元说方法应该是public/受保护的虚拟或受保护的内部虚拟,抓到22一点点?

I have tried changing the type of method (private/protected etc) but there is often conflicting needs by fxCop and the unit tests, fxcop saying it doesnt like contructors with virtual methods but the unit saying that the methods should be public/protected virtual or protected internal virtual, catch 22 a lil bit?

因此,感谢您对解决此fxcop规则的任何帮助,谢谢.

So any help to fix this fxcop rule would be appreciated, thanks.

(该错误发生在标有>的行,虚拟get set方法以及更改后的其他>所在的行)

(The error occurs on the line marked with a >, the virtual get set method and when changed the other > is where it complains)

推荐答案

这很简单:不要这样做那样".相反:

This is easy: "don't do that". Instead:

private string _description;
public Region(string description)
{
    Check.Require(!string.IsNullOrEmpty(description));
    _description = description;
}

protected Region()
{
}

[DomainSignature]
public virtual string Description {
    get {return _description;} 
    set {_description = value;}
}

这篇关于构造函数中的可重写方法-帮助修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆