是否可以通过覆盖 .NET 中的属性来添加访问器?

发布于 2024-07-13 05:34:17 字数 837 浏览 8 评论 0原文

可以做这样的事情吗?

class A
{
    public virtual string prop
    {
        get
        {
            return "A";
        }
    }
}
class B: A
{
    private string X;
    public override string prop
    {
        get
        {
            return X;
        }
        set
        {
            X = value;
        }
    }
}

也就是说,基类提供仅具有 GET 访问器的虚拟属性,但子类重写 GET 并还提供 SET。

当前的示例无法编译,但也许我在这里遗漏了一些东西。

添加:澄清一下,不,我不想用新的重新定义。 我想添加一个新的访问器。 我知道它不在基类中,所以它不能被覆盖。 好吧,让我尝试解释一下如果没有语法糖的话它会是什么样子:

class A
{
    public virtual string get_prop()
    {
            return "A";
    }
}
class B: A
{
    private string X;
    public override string get_prop()
    {
        return X;
    }
    public virtual string set_prop()
    {
        X = value;
    }
}

Is it possible to do something like this?

class A
{
    public virtual string prop
    {
        get
        {
            return "A";
        }
    }
}
class B: A
{
    private string X;
    public override string prop
    {
        get
        {
            return X;
        }
        set
        {
            X = value;
        }
    }
}

That is, the base class provides a virtual property with only a GET accessor, but the child class overrides the GET and also provides a SET.

The current example doesn't compile, but perhaps I'm missing something here.

Added: To clarify, no I don't want to redefine with new. I want to add a new accessor. I know it wasn't in the base class, so it cannot be overriden. OK, let me try to explain how it would look without the syntactic sugar:

class A
{
    public virtual string get_prop()
    {
            return "A";
    }
}
class B: A
{
    private string X;
    public override string get_prop()
    {
        return X;
    }
    public virtual string set_prop()
    {
        X = value;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

靖瑶 2024-07-20 05:34:17

不,这是不可能的。 不幸的是,您甚至无法覆盖和更改访问器级别(例如,从受保护公共),如MSDN。 我建议您考虑稍微重构代码/类,并寻找替代方法来完成此任务,例如使用派生类中的 SetProperty 方法使用 protected 修饰符声明 set 访问器。

No, this is not possible. Unfortunately you can not even override and change the accessor level (from protected to public for example), as documented on MSDN. I would recommend that you consider restructuring the code/class slightly and look for an alternative way to accomplish this task such as declaring the set accessor with the protected modifier using a SetProperty method in the derived class.

美胚控场 2024-07-20 05:34:17

不,没有办法做到这一点。 想想你的虚拟属性的语法糖是如何处理的,即它被转换为:

public virtual string get_prop();

没有要重写的 set_Prop 方法,并且你不能重写不存在的方法。

No, there is no way to do this. Think about how the syntactic sugar of your virtual property is being dealt with, i.e. it gets converted to:

public virtual string get_prop();

There is no set_Prop method to override, and you can't override a method that doesn't exist.

﹂绝世的画 2024-07-20 05:34:17

您可以通过在派生类中使用“new”关键字来隐藏基类实现。 以下内容应该可以成功编译:

class A
{
    public virtual string prop
    {
        get
        {
            return "A";
        }
    }
}
class B : A
{
    private string X;
    public new string prop
    {
        get
        {
            return X;
        }
        set
        {
            X = value;
        }
    }
}

You can hide the base class implementation by using the 'new' keyword in your derived class. The following should compile succesfully:

class A
{
    public virtual string prop
    {
        get
        {
            return "A";
        }
    }
}
class B : A
{
    private string X;
    public new string prop
    {
        get
        {
            return X;
        }
        set
        {
            X = value;
        }
    }
}
つ低調成傷 2024-07-20 05:34:17

引入一个新领域有点混乱。 无论哪种方式,您都需要小心不要破坏多态性和继承(如果基类与私有字段对话怎么办?)。

关于在重写时添加新的访问器; 简单的答案是“不,你不能”。 当您重写时,只能影响现有的访问器,因为这是在成员的虚拟表中定义的内容(重写的成员仍然由基/声明类“拥有”,而不是重写类)。

一种选择(不理想)是重新声明属性,但您仍然需要一种与基类对话的方法。 因此,如果基类中的访问器受到保护

class A
{
    public string prop
    {
        get;
        protected set;
    }
}
class B : A
{
    public new string prop
    {
        get { return base.prop; }
        set { base.prop = value; }
    }
}

Introducing a new field smacks of messy. And either way you need to be careful not to break polymorphism and inheritance (what if the base-class talks to the private field?).

With regard to adding a new accessor while overriding; the simple answer is "no, you can't". When you override, you can only impact the existing accessors, since that is what is defined in the virtual-table for the member (an overridden member is still "owned" by the base/declaring class, not the overriding class).

One option (not ideal) is to re-declare the property, but you would still need a way to talk down to the base-class. So if the accessor in the base-class was protected:

class A
{
    public string prop
    {
        get;
        protected set;
    }
}
class B : A
{
    public new string prop
    {
        get { return base.prop; }
        set { base.prop = value; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文