Declarative DependencyProperety registration Most properties have simple requirements so it should be possible to register their Dependency Property fields declaratively. 

I made a small experiment and it seems to work just fine. The first attempt supports DefaultValue and Validation attributes.

It looks a little bit like JavaScript ;-)



The new DependencyPropertyBuilder loops over fields of type DependencyProperty. For each field it gets the necessary data with reflection from the property and its attributes.

class DependencyPropertyBuilder
{
    public static void RegisterFor&lt;T&gt;() where T : DependencyObject
    { 
        var fields = typeof(T).GetFields().Where(x =&gt; x.FieldType == typeof(DependencyProperty));
        foreach (var field in fields)
        {
            var property = typeof(T).GetProperty(Regex.Replace(field.Name, "Property$", string.Empty));
            field.SetValue(
                null, 
                DependencyProperty.Register(
                    property.Name, 
                    property.PropertyType, 
                    typeof(T), 
                    new PropertyMetadata()
                    {
                        DefaultValue = new Func&lt;object&gt;(() =&gt;
                        {
                            var defaultValue = property.GetCustomAttribute(typeof(DefaultValueAttribute)) as DefaultValueAttribute;
                            if (defaultValue != null)
                            {
                                return defaultValue.Value;
                            }

                            // Return the default value for the type.
                            if (property.PropertyType.IsValueType)
                            {
                                return Activator.CreateInstance(property.PropertyType);
                            }
                            return null;
                        })(),
                    }, 
                    new ValidateValueCallback(v =&gt; new Func&lt;bool&gt;(() =&gt; 
                    {
                        var validations = property.GetCustomAttributes(
                            typeof(ValidationAttribute))?.Cast&lt;ValidationAttribute&gt;() 
                            ?? Enumerable.Empty&lt;ValidationAttribute&gt;();
                        // Evaluate all validators.
                        return validations.All(x =&gt; x.IsValid(v));
                    })())
                )
            );
        }
    }
}


The new test object is now really short:

class TestObject : DependencyObject
{
    public static readonly DependencyProperty CountProperty;

    static TestObject()
    {
        DependencyPropertyBuilder.RegisterFor&lt;TestObject&gt;();
    }

    [DefaultValue(5)]
    [Range(0, 15)]
    public int Count
    {
        get { return CountProperty.GetValue&lt;int&gt;(this); }
        set { CountProperty.SetValue(this, value); }
    }
}


The usage is the same:

var testObject = new TestObject();
testObject.Count.Dump("Default");

testObject.Count = 8;
testObject.Count.Dump("Changed");

testObject.Count = 22; // bam! out-of-range
Few notes :


// Return the default value for the type.
if (property.PropertyType.IsValueType)
{
    return Activator.CreateInstance(property.PropertyType);
}
return null;



You can use ternary operator here

// Return the default value for the type.
return property.PropertyType.IsValueType
    ? Activator.CreateInstance(property.PropertyType)
    : null;


Those })() at the end of your functors are a little bit confusing in my opinion it will be a clearer if you call .Invoke() explicitly }).Invoke()


var validations = property.GetCustomAttributes(
typeof(ValidationAttribute))?.Cast&lt;ValidationAttribute&gt;()
?? Enumerable.Empty&lt;ValidationAttribute&gt;();



You should be able to specify the type argument explicitly to avoid the .Cast&lt;T&gt;

var customValidationAttributes = property.GetCustomAttributes&lt;ValidationAttribute&gt;();
var validations = customValidationAttributes ?? Enumerable.Empty&lt;ValidationAttribute&gt;();