Customizing property accessor accessibility

 

Introduction

By default, get and set accessors have the same visibility or access level - that of the property or indexer they belong to.  However, it can be useful to restrict access to one of these accessors (in general, this means restricting the accessibility of the set accessor, and keeping the get accessor publicly accessible).

 

 

Restrictions on access modifiers on accessors

To restrict the visibility of a get or set accessor, select an attribute in the explorer and then in the auxiliary window, associate either the {CsPropertyVisibilityGet} or the {CsPropertyVisibilitySet} tagged value, with the desired access modifier as the only parameter (see below).

 

The {CsPropertyVisibilitySet} tagged value in the auxiliary window

 

Steps:

1.      To associate the {CsPropertyVisibilitySet} or {CsPropertyVisibilityGet} tagged value to the element selected in the explorer, click on the "Associate a tagged value" icon in the auxiliary window and select it in the dropdown list.

2.      The tagged value then appears in the auxiliary window (in the screenshot above, the {CsPropertyVisibilitySet} tagged value).  In the example above, the "protected" access modifier has also been applied.

 

During property generation, the access modifier indicated as being the parameter of the {CsPropertyVisibilityGet} or {CsPropertyVisibilitySet} tagged value will be positioned on the corresponding get or set accessor.

 

 

In the following example, a property called "Attribute" defines a get accessor and a set accessor.  The get accessor is given the same accessibility level as the property itself (in our example, public), while the set accessor is explicitly restricted through the application of the protected access modifier to the accessor itself.

 

UML Model

Generated C#