This smells like a compiler error. Attribute classes are “special” classes so that they can be used as metadata. The C # compiler allows you to use them differently than regular classes, and therefore we can assume that there is a (partial) custom implementation in the C # compiler to compile the use of attribute classes. (Can anyone check this out for mono?)
However, I did some tests and found that only when using the attribute constructor, which sets the default value for the parameter as null without specifying a user-defined value for this attribute, does the compiler give us an error. My test code is:
class TestAttribute : Attribute { public TestAttribute(object test = null) { }
(Tested with VS 2010 under .NET 4.0, can anyone verify this with mono?)
Note that attributes already allow you to access properties as if they were optional, so you could make your optional parameter a property (if it’s not already, and remove it from the constructor. This still allows you to write [ Test (null, MyProperty = null)]
Polity Nov 28 '11 at 3:00 2011-11-28 03:00
source share