Is it possible to check the .NET Attibute setting?

I am wondering if it is possible to check the options for (custom) .net attributes. eg: If I had an attribute that takes a positive integer, can I get a compile-time error when I feed in a negative value?

[DonkeyAttribute (1)] // OK

[DonkeyAttribute (-828)] // error

For this example I could use an unsigned integer (but is it not cls-compliant I beleive?) Recommendations?

+1


source to share


4 answers


I don't think this is ok, however this article describes a solution using PostSharp . Not sure if it suits your purpose, but give it away!



0


source


You can apply this with unit tests; a similar solution to the one I suggested for this question is possible.



+1


source


Directly? Not. Without rewriting csc or vbc. Most people have done this check at runtime.

However, a bit of Googling came up with this PostSharp Aspects blog post . This is not technically proven with the compiler, but it does provide a compile-time check. You can check it out here . More notes on PostSharp from the same author can be found here .

0


source


You can create your own attribute by inheriting from System.Attribute. In the custom constructor, you have to check the parameters.

0


source







All Articles