使用 InitMembers() 重写构造函数中的方法

发布于 2024-07-27 14:52:35 字数 774 浏览 1 评论 0原文

我已经在这里几乎所有我遇到的领域使用了方法已经有可重写的方法并设法修复它们,但有一个部分该方法在不同上下文的代码段上不能以相同的方式工作:

    public Employee()
    {
        this.InitMembers();
    }

    private void InitMembers()
    {
        // Init the collection so it's never null
        this.Territories = new List<Territory>();
    }
    public Employee(string firstName, string lastName): this()
    {
        this.reffirstName = firstName;
        this.reflastName = lastName;
    }
>   public virtual IList<Territory> Territories { get; protected set; }

再次出现 > > 。 是导致错误的代码,但是我确实获得了“转换为自动属性”的智能感知选项,该选项只是将代码恢复到启动时的状态,而不是解决问题。 有谁知道需要对此部分进行哪些修改才能消除 fxcop 违规?

I have carried the method here on almost all of the areas where I have had overridable methods and managed to fix them but there is one part where the method doesnt work in the same way on a different contexted piece of code:

    public Employee()
    {
        this.InitMembers();
    }

    private void InitMembers()
    {
        // Init the collection so it's never null
        this.Territories = new List<Territory>();
    }
    public Employee(string firstName, string lastName): this()
    {
        this.reffirstName = firstName;
        this.reflastName = lastName;
    }
>   public virtual IList<Territory> Territories { get; protected set; }

Where again the > is the code causing the error, I do however get an intellisense option to "Convert to auto property", which simply reverts the code to when it was started and not fixing the problem.
Anyone know what modifications need to be made to this part to elimiate the fxcop violation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2024-08-03 14:52:35

出现该错误是因为您的私有构造函数正在调用可以从派生类重写的方法。 要修复该警告,您需要从构造函数中删除对虚拟方法的所有调用。

在您列出的示例中,InitMembers 使用“this.Territories”,这导致了违规。 根据您后来的评论,您已经添加了一个私人成员 - 请改用它。

The error appears because your private constructor is calling a method that can be overridden from a derived class. To fix the warning, you need to remove any calls to virtual methods from within the constructor.

In the example you list, InitMembers uses 'this.Territories', which is causing the violation. According to your later comment you have added a private member - use that instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文