ObsoleteAttribute如何以及为何不允许使用属性访问器?

Tin*_*ter 8 c# attributes

考虑这种情况:

public class C
{
    private int _foo;

    public int Foo
    {
        get { return _foo; }

        [Obsolete("Modifying Foo through the setter may corrupt hash tables.  "
            + "Consider using the method 'ModifyFoo' instead.")]
        set { _foo = value; }
    }

    public C ModifyFoo( int foo )
    {
        // return a new instance of C
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个不编译:

错误CS1667:属性"已过时"在属性或事件访问器上无效.它仅对'class,struct,enum,constructor,method,property,indexer,field,event,interface,delegate'声明有效.

将属性专门应用于访问器对于任何其他属性都是完全正确的(前提AttributeTargets.Method是在其用法中设置,这是真的ObsoleteAttribute).

Han*_*ant 7

显然,编译器明确禁止其使用,这不是偶然的.Hmya,为什么?编译器中限制的解决方法似乎不太可能.我猜他们决定禁止它以避免混淆程序员获得警告.除非信息精心制作,否则会出现不一致的情况,这取决于属性的使用情况.