Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# AttributeUsage for Specific Class

Is it possible to have something like AttributeUsage to restrict the use of an attribute to a specific class (not just AttributeTargets.Class - that would be any class)?

like image 655
Alex Avatar asked Sep 17 '09 20:09

Alex


2 Answers

One way to accomplish this, if you have access to the specific class, is detailed by Marc Gravel here: http://marcgravell.blogspot.com/2009/06/restricting-attribute-usage.html. Basically you implement the attribute as a protected class of the specific type. Then it can only be used by that type.

like image 189
sgriffinusa Avatar answered Sep 28 '22 08:09

sgriffinusa


No. There is nothing in the framework that would do this.

However, the code that uses the attribute in question could always check to make sure that the class's type is the specific class (or one of its subclasses).

Attributes, by themselves, do nothing - so this should have the same effect.

like image 31
Reed Copsey Avatar answered Sep 28 '22 07:09

Reed Copsey