What if I don't have a backing field?

Oct 2, 2008 at 7:52 AM
A user wrote in to ask me how to use ActiveSharp in cases where the property is not backed by a field.  E.g. it delegates to another class.

I put together this sample, to demonstrate a solution.

Key points to note:

  • It solves the problem by adding little 1-byte backing fields (I'm calling them "Tags") into the class.  They are never used for anything other than ActiveSharp's need to have a backing field to identify the property.  The actual get and set logic still delegates off to the other class.  This works because all ActiveSharp requires is that the property setter must use a field by ref (and that no other property may use the same field by ref).  The field doesn't have to store the actual value. 
  • The tags are "empty" structs.  This is one of the few known uses for "empty" structs.
  • It will always send the notification, even if the property has simply been set to its old (existing) value.  Contrast that with "normal" use of ActiveSharp, which would not send the notification if the property has been set to its existing value.