Steven Schveighoffer wrote:
Annotations have more usages than just how to serialize. Some uses I've
seen in C#:
* hints to an IDE about a GUI component (what it does, what properties
to allow setting via the visual builder)
* hints to the debugger about when to skip debugging certain functions
(to avoid stepping into mundane crap such as property getters).
* hints to another program about which classes would be interesting when
dynamically loading a library
In Actionscript (and the Flex framework), one very handy use of
annotations is to mark a public field as "bindable".
class MyClass {
[Bindable]
public var MyField:int = 0;
}
In this example, whenever the "MyField" value is updated, a
property-change event will be send to all listeners. The XML-based Flex
framework uses those annotations to create (unidirectional or
bidirectional) bindings between variables.
<Window>
<HSlider id="mySlider"/>
<Image
source="..."
width="{mySlider.value}"
height="{mySlider.value}"
/>
</Window>
This creates a window with two controls, a horizontal numeric slider and
an image. Whenever the user drags the slider control, the width and
height of the image automatically update themselves.
The reason this works is that the "value" field of the "HSlider"
object is marked with the "Bindable" annotation. The compiler silently
converts the field into a property getter/setter pair, and the setter
sends out property-change events whenever called.
(Good thing Actionscript properties exist, with a syntax identical to
normal fields, or else the automatic data binding wouldn't work!)
The cool thing that makes this work is that the compiler can perform
code transformation based on the existence of various annotations.
--benji