Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an Attribute be set only on the Setter of an Auto-Implemented Property?

Currently I have this:

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

However, I want this attribute to decorate only the setter, not the getter. Is there a way to do that?

like image 444
David Avatar asked Mar 23 '12 14:03

David


People also ask

What is an auto-implemented property?

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.

What is the point of auto-implemented properties in C#?

Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects.


1 Answers

Syntactically, the following is allowed:

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

The rest is up to (your?) SomeCustom Attribute.

It would of course have to be a method attribute, not a property attribute.

like image 68
Henk Holterman Avatar answered Oct 06 '22 01:10

Henk Holterman