这些构造函数中的虚拟成员调用警告是否合法?
我在设置 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
}
}
I'm getting Resharper warnings here in the base form where I set the EditMyDataObject property, and in the extended form where I grab the Text value and then restore it afterwards.
In both cases nothing is blowing up when I run the app. I don't think there's any potential for problems as implemented because my overridden property doesn't depend on anything initialized in the constructor, but would appreciate a 2nd opinion before gagging 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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要每个派生类以不依赖于该派生类的构造函数中初始化的变量的方式重写此虚拟成员,您就是安全的。问题是,您无法知道从基类派生的每个类都具有这样的行为。因此,该警告是合法的。
As long as each derived class overrides this virtual member in a way that it doesn't rely on variables that are initialized in the constructor of that derived class, you are safe. Problem is, you can't know that every class that derives from your base class behaves like this. Because of that, the warning is legit.