Offler
Offler

Reputation: 1223

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?

Upvotes: 10

Views: 1428

Answers (6)

Aleksandar Toplek
Aleksandar Toplek

Reputation: 2821

How about using #warning + Unit testing? In this way, whenever you run Unit tests, an warning will be generated (or you could just use Debug.Fail instead of #warning)

Upvotes: 0

user2535425
user2535425

Reputation:

You could write some code that runs on application start which uses reflection and would then throw runtime exceptions if an attribute was used without the proper match but I believe that's as far as you can go and personally I wouldn't consider that a good approach as you would need to run the application once to make sure it complies with your rules.

Also, take a look at PostSharp which may help you.

Upvotes: 0

Sebastian Negraszus
Sebastian Negraszus

Reputation: 12195

As far as I know, there is no way to check for attributes at compile time.

I recently needed to enforce something similar (all classes derived from a certain base class need certain attributes). I ended up putting a manual check (with [Conditional("DEBUG")]) using reflection into the constructor of the base class. This way, whenever someone creates an instance of a class with missing attributes, they get an exception. But this might not be applicable in your case, if your classes do not all derive from the same class.

Upvotes: 0

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

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.

Upvotes: 1

Tigran
Tigran

Reputation: 62248

Another option is using some AOP techniques. Like for example:

PostSharp.

Using it you can at compilation analyze yur code and emit a error if some condition does not sutisfies your requirements.

For concrete example on attributes, can have a look on :

PostSharp 2.1: Reflecting Custom Attributes

Upvotes: 3

Bas
Bas

Reputation: 27085

Unfortunately you cannot generate custom compiler warnings from attributes. Some attributes like System.ObsoleteAttribute will generate a warning or error, but this is hard-coded into the C# compiler. You should find another solution to your problem, maybe letting Some3rdPartyAttribute inherit from RequiredAttribute?

Otherwise you have to change the compiler.

Upvotes: 4

Related Questions