【发布时间】:2013-08-01 10:24:50
【问题描述】:
我正在用 C# 制作软件。我正在使用一个抽象类Instruction,它包含以下代码:
protected Instruction(InstructionSet instructionSet, ExpressionElement newArgument,
bool newDoesUseArgument, int newDefaultArgument, int newCostInBytes, bool newDoesUseRealInstruction) {
//Some stuff
if (DoesUseRealInstruction) {
//The warning appears here.
RealInstruction = GetRealInstruction(instructionSet, Argument);
}
}
和
public virtual Instruction GetRealInstruction(InstructionSet instructionSet, ExpressionElement argument) {
throw new NotImplementedException("Real instruction not implemented. Instruction type: " + GetType());
}
所以 Resharper 告诉我,在标记的行中,我正在“在构造函数中调用虚拟方法”,这很糟糕。我了解调用构造函数的顺序。 GetRealInstruction 方法的所有覆盖如下所示:
public override Instruction GetRealInstruction(InstructionSet instructionSet, ExpressionElement argument) {
return new GoInstruction(instructionSet, argument);
}
所以它们不依赖于类中的任何数据;他们只是返回取决于派生类型的东西。 (所以构造函数的顺序不会影响它们)。
那么,我应该忽略它吗?我宁愿不;那么谁能告诉我如何避免这个警告?
我不能巧妙地使用委托,因为GetRealInstruction 方法多了一个重载。
【问题讨论】:
标签: c# inheritance constructor resharper