Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Obsolete attribute on only a getter or a setter of a property

Tags:

c#

Is it possible to use Obsolete attribute on only a getter or a setter of a property?

I would like to be able to do something like this:

public int Id {     get { return _id;}     [Obsolete("Going forward, this property is readonly",true)]     set { _id = value;} } 

but obviously that will not build. Is there a work around that allows me to apply this attribute to just the setter?

like image 814
wusher Avatar asked Nov 05 '10 17:11

wusher


People also ask

What is obsolete attribute in C#?

An obsolete attribute, in C#, is a declarative tag used while declaring a type or a member of a type to indicate that it should no longer be used.

Can we use setter without getter?

That code only requires the setter, not the getter. The interface documents that fact. An interface is just a facility for declaring a group of operations that are "atomically needed" (e.g. if you need to call method A, you'll need to read property B and set property C). So as always, it depends.

How do you mark a method obsolete in C#?

The Obsolete attribute decorates a program element by putting the word “Obsolete” above it inside square brackets. Since it is an attribute, we can use either Obsolete or ObsoleteAttribute. [Obsolete] − is a no parameter constructor and is a default using this attribute.

What is system obsolete?

Obsolete refers to outdated computer hardware, software, technology, services or practices that are no longer used, even if they are in working condition. A technology often becomes obsolete when replaced by a newer or better technology.


1 Answers

I think that this can't be done because, for some reason, it has been specifically disallowed for the Obsolete attribute. According to the rules defined around attribute targets, there doesn't appear to be any reason that the Obsolete attribute would not be valid on a property get or set accessor. In order to apply an attribute to a property set accessor, that attribute must be applicable to either a method, parameter, or return value target. If you look at the Obsolete attribute, you can see that "method" is one of the valid targets for that attribute.

In fact, you can define your own attribute with the same valid targets as the Obsolete attribute with the AttributeUsage attribute, and you'll find that you can apply it to a property get or set accessor whereas you cannot apply the Obsolete attribute.

[AttributeUsage(AttributeTargets.Method)] class MyMethodAttribute : Attribute { }  class MyClass {     private int _Id;      public int Id     {         get { return _Id; }          [MyMethodAttribute] // this works because "Method" is a valid target for this attribute         [Obsolete] // this does not work, even though "Method" is a valid target for the Obsolete attribute         set { _Id = value; }     } } 

If you try creating your own attribute that is not valid on a property set accessor and you apply it there, then you may notice the error message is slightly different. The error message for your custom attribute will be "Attribute 'YourCustomAttribute' is not valid on this declaration type.", whereas the error message for the Obsolete attribute is "Attribute 'Obsolete' is not valid on property or event accessors." The fact that the error message is different makes me believe that this is a rule that is, for whatever reason, explicitly built into the compiler for the Obsolete attribute, rather than relying on the AttributeUsage attribute that is, supposedly, applied to the Obsolete attribute.

like image 125
Dr. Wily's Apprentice Avatar answered Oct 18 '22 09:10

Dr. Wily's Apprentice