为什么C#中的完整属性只能被一个getter覆盖,但它仍然可以被设置?

时间:2018-05-16 17:48:39

标签: c# visual-studio visual-studio-2017 c#-7.3

我遇到了令我惊讶的行为。给出以下两个类:

class Parent
{
    public virtual bool Property { get; set; }
}

class Child : Parent
{
    public override bool Property { get => base.Property; }
}

我可以写这样的代码:

Child child = new Child();
child.Property = true; // this is allowed

IDE也让它变得混乱,因为虽然它允许赋值,但它也表明被覆盖的属性是只读的:

enter image description here

此外,只有在我使用基类'getter:

时才允许此覆盖

enter image description here

这里发生了什么?

1 个答案:

答案 0 :(得分:7)

我会对此采取行动。

看起来这可能只是Intellisense的一个错误,它无法找到自动属性的基本实现。代码是有效的,也是有道理的 - 这是表达你的例子的另一种方式。

Child child = new Child();
child.SetProperty(true);

class Parent
{
    private bool _property;

    public virtual bool GetProperty() => _property;
    public virtual void SetProperty(bool value) => _property = value;
}

class Child : Parent
{
    public override bool GetProperty() => base.GetProperty();
}

通过这种表示,现在很明显为什么覆盖GetProperty很好。这是您的代码的相关IL:

Main:
IL_0000:  newobj      Child..ctor
IL_0005:  ldc.i4.1
IL_0006:  callvirt    Parent.set_Property
IL_000B:  ret

Parent.get_Property:
IL_0000:  ldarg.0
IL_0001:  ldfld       Parent.<Property>k__BackingField
IL_0006:  ret

Parent.set_Property:
IL_0000:  ldarg.0
IL_0001:  ldarg.1
IL_0002:  stfld       Parent.<Property>k__BackingField
IL_0007:  ret

Parent..ctor:
IL_0000:  ldarg.0
IL_0001:  call        System.Object..ctor
IL_0006:  ret

Child.get_Property:
IL_0000:  ldarg.0
IL_0001:  call        Parent.get_Property
IL_0006:  ret

Child..ctor:
IL_0000:  ldarg.0
IL_0001:  call        Parent..ctor
IL_0006:  ret

这是我的版本:

Main:
IL_0000:  newobj      Child..ctor
IL_0005:  ldc.i4.1
IL_0006:  callvirt    Parent.SetProperty
IL_000B:  ret

Parent.GetProperty:
IL_0000:  ldarg.0
IL_0001:  ldfld       Parent._property
IL_0006:  ret

Parent.SetProperty:
IL_0000:  ldarg.0
IL_0001:  ldarg.1
IL_0002:  stfld       Parent._property
IL_0007:  ret

Parent..ctor:
IL_0000:  ldarg.0
IL_0001:  call        System.Object..ctor
IL_0006:  ret

Child.GetProperty:
IL_0000:  ldarg.0
IL_0001:  call        Parent.GetProperty
IL_0006:  ret

Child..ctor:
IL_0000:  ldarg.0
IL_0001:  call        Parent..ctor
IL_0006:  ret     

请注意,这与public override bool Property { get; }不同,它是指示编译器为同名支持属性生成单个getter覆盖的简写,没有提及预先存在的setter。然而,有实际规范经验的人肯定能够提供更多关于此的信息。