使用 InitMembers() 重写构造函数中的方法
我已经在这里几乎所有我遇到的领域使用了方法已经有可重写的方法并设法修复它们,但有一个部分该方法在不同上下文的代码段上不能以相同的方式工作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
出现该错误是因为您的私有构造函数正在调用可以从派生类重写的方法。 要修复该警告,您需要从构造函数中删除对虚拟方法的所有调用。
在您列出的示例中,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.