Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force the usage of an attribute on properties, if they already have another attribute

I want to force the usage of an attribute, if another attribute is used. If a special 3rd party attribute is attached to a property, this attribute also needs to be given to a property. Is there any possibility of doing this?

For example:

[Some3rdPartyAttribute("...")]
[RequiredAttribute("...)]
public bool Example{get; set;}

should bring no compile error,

[Some3rdPartyAttribute("...")]
public bool Example{get; set;}

should bring a compile error or warning.

The attribute itself is definded like the example from http://msdn.microsoft.com/en-US/library/z919e8tw(v=vs.80).aspx itself . But how to force the usage of the attribute if another attribute is used?

like image 833
Offler Avatar asked Jul 10 '13 09:07

Offler


People also ask

What is the difference between an attribute and a property in Python?

Attributes are described by data variables for example like name, age, height etc. Properties are special kind of attributes which have getter, setter and delete methods like __get__, __set__ and __delete__ methods.

What is AttributeUsage C#?

Determines how a custom attribute class can be used. AttributeUsage is an attribute that can be applied to custom attribute definitions to control how the new attribute can be applied. So it basically gives the compiler some extra information about the attribute class you will implement.

What are managed attributes in python?

You can use managed attributes, also known as properties, when you need to modify their internal implementation without changing the public API of the class. Providing stable APIs can help you avoid breaking your users' code when they rely on your classes and objects.

Why do we need attributes in C#?

Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection.


1 Answers

You can make a console app, that will iterate trough all types in your assembly trough reflection, check if the rule is satisfied and return 0 if it is, and some other error code and output error if the rule is broken.

Then make this console app run as post-build task.

like image 155
Ondrej Svejdar Avatar answered Sep 25 '22 07:09

Ondrej Svejdar