2012-03-23 61 views
6

目前我有这样的:只能在Auto-Implemented Property的Setter上设置属性吗?

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

不过,我想这个属性来装点只有二传手,而不是吸气。有没有办法做到这一点?

+0

基于问题[这里](http://stackoverflow.com/questions/1161427/apply-attribute-to-property-get-set-methods-via-batch-file),我倾向于说你可能可以,尽管我从来没有理由在实践中做到这一点。 – eouw0o83hf 2012-03-23 15:03:19

+0

getter和setter实际上是方法,所以为什么不呢? – 2012-03-23 15:04:35

回答

10

在语法上,下面是允许的:

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

剩下的就到(你的?)SomeCustom属性。

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

1
public string Name 
{ 
    get; 
    [SomeCustomAttribute] 
    set; 
} 
相关问题