【问题标题】:Can an Attribute be set only on the Setter of an Auto-Implemented Property?只能在自动实现的属性的设置器上设置属性吗?
【发布时间】:2012-03-23 14:58:13
【问题描述】:

目前我有这个:

[SomeCustomAttribute]
public string Name { get; set; }

但是,我希望这个属性只装饰 setter,而不是 getter。有没有办法做到这一点?

【问题讨论】:

  • 基于问题here,我倾向于说你可能可以,尽管我在实践中从来没有理由这样做。
  • getter 和 setter 实际上是方法,为什么不呢?

标签: c# custom-attributes


【解决方案1】:

从句法上讲,以下是允许的:

 public string Name { get; [SomeCustomAttribute] set; }

其余的取决于(你的?)SomeCustom 属性。

它当然必须是方法属性,而不是属性属性。

【讨论】:

    【解决方案2】:
    public string Name
    {
        get;
        [SomeCustomAttribute]
        set;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-12
      • 1970-01-01
      • 2015-02-27
      • 2013-02-28
      • 2011-02-10
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多