Attributes, CLS compliance and VB 2005

Since I’m answering questions today, I might as well address Robert‘s question as to why VB doesn’t support attributes that take 1-dimensional arrays. Actually, not only does VB not support attributes that take 1-dimensional arrays, we also don’t support attributes that take parameters typed as Object. So why not? Time, mostly, but the CLS comes in to play here as well.

Attributes that take 1-dimensional arrays and Object are not CLS compliant, as per CLS rule #34 (see pg. 118 of Jim Miller’s excellent Common Language Infrastructure Annotated Standard book). Interestingly, though, the C# compiler does not warn you when you declare attribute constructors that use the non-CLS compliant types but you declare your assembly to be CLS compliant. My take on it is that they have a bug in their compiler and that they should warn you. However, I suspect they might also make a claim that the vague wording of the rule makes their compiler behavior correct – the rule states that other types cannot be “encoded,” so I guess they could argue that it’s not the attribute definition that’s non-CLS compliant, but some theoretical use of it. However, the CLS rules generally talk about prohibit you from declaring something non-CLS compliant not using something that’s non-CLS compliant, so I’m not sure I would lend this argument (if, indeed, they actually wanted to make it) much credence.

Anyway, language lawyering aside, the issue was that attributes came online very late in VB 2002, and we ended up not support 1-dimensional arrays and object values in attributes for time reasons. We figured it wouldn’t be that big of a deal because, hey, they’re not CLS compliant! Of course, since there’s no warning from the C# compiler about using these types in attributes, people expect them to work in VB and, when push comes to shove, the CLS argument just isn’t very satisfying. Which is all a long way of saying that we’re supporting them in VB 2005.

And I’m going to have to go now and test to see whether we give you a CLS warning…

1 thought on “Attributes, CLS compliance and VB 2005

  1. Eric Mutta

    I recently ran up against a situation where being able to use 1-D arrays in attributes would have been useful. The work-around I used was to apply the same attribute multiple times (once for each element of the array) but its good to know this will be unnecessary later! 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *