这些构造函数中的虚拟成员调用警告是否合法?

发布于 2024-12-20 07:46:51 字数 2217 浏览 3 评论 0原文

我在设置 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 技术交流群。

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

发布评论

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

评论(1

昨迟人 2024-12-27 07:46:51

只要每个派生类以不依赖于该派生类的构造函数中初始化的变量的方式重写此虚拟成员,您就是安全的。问题是,您无法知道从基类派生的每个类都具有这样的行为。因此,该警告是合法的。

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.

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