DependencyAutoProperty Mutator

Mutator that turns an auto-property into a property that supports DependencyProperty: the mutator adds a static field DependencyProperty and converts the property body to use GetValue/SetValue. It supports the DefaultValueAttribute to specify the default value of the property.
  • Defined in: CciSharp.DependencyAutoProperty.dll

Example

  • original code
using System.Windows;

class Foo : DependencyObject {
    [DependencyProperty]
    public int Value {get; set;}
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
class DependencyPropertyAttribute : Attribute { }
  • rewritten code
class Foo : DependencyObject {
    public static readonly DependencyProperty ValueProperty 
        = DependencyProperty.Register("Value", typeof(int), typeof(Foo));
    public int Value {
        get { return (int)base.GetValue(ValueProperty); }
        set { base.SetValue(ValueProperty, value); }
    }
}

Requirements:

  • an non-virtual instance auto-property with a public getter and setter
  • marked with a DependencyPropertyAttribute. It does not matter in which namespace or assembly this attribute lives, the tool only does name matching on the attribute
  • declaring type inherits from System.Windows.DependencyObject.

Default Value support

The attribute can have a DefaultValue property to specify the default value. The rewritter registers that values through a PropertyMetadata element.
  • original code
using System.Windows;

class Foo : DependencyObject {
    [DependencyProperty(DefaultValue = 10)]
    public int Value {get; set;}
}
class DependencyPropertyAttribute : Attribute { 
    public object DefaultValue {get;set;}
}
  • rewritten code
class Foo : DependencyObject {
    public static readonly DependencyProperty ValueProperty 
        = DependencyProperty.Register("Value", typeof(int), typeof(Foo), new PropertyMetadata(10));
    ...
}

Validation callback support

The attribute can have a Validate boolean property to specify that the property have a validation method. The rewritter looks for a static method named Validate<Property Name> that takes a parameter of the property type:
static int ValidateValue(int value) {...}
  • original code
using System.Windows;

class Foo : DependencyObject {
    [DependencyProperty(Validate = true)]
    public int Value {get; set;}
    // heads up on the naming convention: Validate + propety name
    static bool ValidateValue(int value) { ... }
}
class DependencyPropertyAttribute : Attribute { 
    bool Validate { get; set; }
}
  • rewritten code
class Foo : DependencyObject {
    public static readonly DependencyProperty ValueProperty 
        = DependencyProperty.Register("Value", typeof(int), typeof(Foo), new PropertyMetadata(10), 
               ValidateValue$Proxy);
    static bool ValidateValue$Proxy(object value) {
        return ValidateValue((int)value);
    }
    static bool ValidateValue(int value) { ... } 
}


Last edited Dec 31, 2009 at 5:00 AM by pelikhan, version 5

Comments

No comments yet.