在这里,我以设置EditMyDataObject属性的基本形式以及在扩展形式中获取Text值,然后再将其还原的方式收到Resharper警告。
在这两种情况下,我运行应用程序时都不会爆炸。我认为实现的问题不大可能,因为我的重写属性不依赖于构造函数中初始化的任何内容,但是在对Resharper作呕之前,我将不胜感激。
public class MyDataObject
{
//Data Members
public MyDataObject()
{
}
}
public class MyDataObjectEx : MyDataObject
{
//Data Members
public MyDataObjectEx()
{
}
public MyDataObjectEx(MyDataObject myDataObject)
{
}
}
public partial class MyDataObjectEditFrm : Form
{
private MyDataObject _myDataObject;
protected virtual MyDataObject EditMyDataObject
{
get { return _myDataObject; }
set { _myDataObject = value; }
}
/// <summary>
/// Parameterless constructor needed for designer support of derived classes.
/// </summary>
protected MyDataObjectEditFrm()
{
InitializeComponent();
}
protected MyDataObjectEditFrm(MyDataObject myDataObject)
{
InitializeComponent();
EditMyDataObject = myDataObject; // Warning: Virtual member call in a constructor
Text = GetDialogNameFromInputParameters()
//Remainder of initialization here
}
GetDialogNameFromInputParameters()
{
//Figure out what the text should be
}
}
public partial class MyDataObjectExEditFrm : MyDataObjectEditFrm
{
private MyDataObjectEx _myDataObjectEx;
protected override MyDataObject EditMyDataObject
{
get { return _myDataObjectEx; }
set
{
if (value == null)
_myDataObjectEx = null;
else _myDataObjectEx = value as MyDataObjectEx ?? new MyDataObjectEx(value);
}
}
public MyDataObjectExEditFrm(MyDataObject myDataObject) : base(myDataObject)
{
//preserve the value computed and set in the base class to prevent the generic form name from the designer overriding it here
string dialogText = Text; // Warning: Virtual member call in a constructor
InitializeComponent();
Text = dialogText; // Warning: Virtual member call in a constructor
//Remainder of additional initialization for extended data here
}
}
最佳答案
只要每个派生类以不依赖于在该派生类的构造函数中初始化的变量的方式覆盖此虚拟成员,就可以保证安全。问题是,您不知道从基类派生的每个类的行为都像这样。因此,警告是合法的。