Dynamic Properties

10/30/2003 1:06:00 PM

Dynamic Properties

Avalon has this concept of dependency properties (used to be called as dynamic properties), which I think is cool. These are reminiscent of Expando properties in Javascript. Instead of keeping the backing store of properties inside an object (derived from DependencyObject), Avalon stores in the object a reference to list of non-default property values.

There are several cool thing about these dynamic properties:

  1. If you don't set these properties to anything other than the default value, they do not take any space in the object. This is great if the object supports hundreds of properties. Actually, this was true for the first version of Avalon; for performance reasons, some properties may be cached inline in the current version.
  2. Other classes can create "attached properties", which are properties that were not originally defined by the class of the object. These new properties are attached onto the original set of properties in the object at runtime (sort of like Javascripts expando properties, but more performant). You see this when you create an Control inside a Canvas panel, and the control gains four new properties, Top, Left, Right and Bottom. If the Control was inside a Dock panel, it would have a Dock property. To set the attached property, you can't use the normal property syntax, since the object doesn't know about this property it never defined, you have to call a static method in the class in which the property is defined and call Canvas.SetTop(control, value).
  3. The values of the properties no longer have to be static. They can be dynamically calculated off of other properties. Any dynamic property can be Animated--the value of a property can change over time. Any dynamic property can be bound to data.
  4. Events can be dynamic properties as well.
  5. Each dynamic property has an DependencyProperty object, which is usually registed in the class that defines it as a static variable. Note: this is no longer the actual syntax, you'll have to look at the sdk.
    public static DependencyProperty FontSizeProperty = 
           RegisterProperty("FontSize", flags, typeof(double), defaultValue);

    Usually, these DependencyObjects that have dynamic properties declare static properties, that call the dynamic properties. Again, this may not be the actual syntax; I believe that it has changed to eliminate the boxing. Then, the class would defined a CLR static property that extracts the dynamic property value, or the developer can call the extract methods directly.

    double FontSize
          get { return (double) this.GetValue(FontSizeProperty); }
          set { this.SetValue(FontSizeProperty, value); }

According to the Microsoft Roadmap, Orcas, the version of VS.NET after Whidbey, will provide additional runtime support for Longhorn. Hopefully, the CLR team or the language teams will provide for the direct runtimes or compiler support for these types of properties as one of the additions to Longhorn, because this feature is a very expressive and powerful, and runtime support would beautify the syntax and reduce code. In addition, dynamic properties would be visible through reflection, if not currently.

Chris talks about this in his blog.






Net Undocumented is a blog about the internals of .NET including Xamarin implementations. Other topics include managed and web languages (C#, C++, Javascript), computer science theory, software engineering and software entrepreneurship.

Social Media