10

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?

6 Answers 6

4

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.

1
  • thx. The 3rd Party attribute (not from us) will hardly inherit from our required atrribute, which should force the programmer to give some addditioanl information if the 3rd party functionallity is used.
    – Offler
    Commented Jul 10, 2013 at 9:53
3

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

3
  • I like the AOP/IOC answer, sadly that is something which i can hardly explain to my boss...
    – Offler
    Commented Jul 10, 2013 at 9:48
  • @Offler: AOP is not good for everything. Consider a fact that, yes, it helpls a lot in automating tasks in declarative way, so you have much less code. But on other side, it hides behavior inside it, so one who is not familiar with it, or when app becomes pretty complex, can lead to complexity handling problems. So if you gonna use it, use it wise.
    – Tigran
    Commented Jul 10, 2013 at 9:51
  • That was what i meant i can hardly explain the usage of AOP to my boss. Also i would like it for some other things like logging ....
    – Offler
    Commented Jul 10, 2013 at 9:55
1

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.

0

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.

0

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.

2
  • I don'T want to change them at runtime. I only want a warning or error during compiletime if an atrribute does not exist
    – Offler
    Commented Jul 10, 2013 at 9:52
  • I've edited my answer as that initial comment was somewhat out of context. In any case, without using reflection and raising errors at runtime I don't see other option myself.
    – user2535425
    Commented Jul 10, 2013 at 10:25
0

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)

2
  • I don't know how to programmatically display warning only if one attribute is there and another isn't with proper text?
    – Offler
    Commented Jul 10, 2013 at 9:51
  • If you are using Unit testing, this could be done easily... just check if method has both attributes, and show warning if it doesn't. Or you could have one test method that checks if all methods that use one attribute, also have other required attributes Commented Jul 10, 2013 at 12:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.