I'm finding lately a lot of code smells related to referencing generic classes in C#. My gripes especially apply to those classes which inherit from DependencyObject and contain DependencyProperties.
The basic problem is that when declaring a dependency property one generally references the current type which is also known as the owner. This works just fine and in general isn't much of a problem for simple non-generic objects except for that when the object contains several dependency properties and then the type name needs to be re-factored in various places (made easier with refactoring in Visual Studio).
public class MyDependencyObject : DependencyObject
{
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(MyDependencyObject), new UIPropertyMetadata(0));
}
What I've been finding lately though is that when combining this burdonsome explicit self referencing practice with extensive use of generics the code really starts to become ugly.
public class MyDependencyObject<TypeA, TypeB, TypeC, TypeD> : DependencyObject
{
public int MyProperty1
{
get { return (int)GetValue(MyPropertyProperty1); }
set { SetValue(MyPropertyProperty1, value); }
}
public static readonly DependencyProperty MyPropertyProperty1 =
DependencyProperty.Register("MyProperty1", typeof(int), typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));
public int MyProperty2
{
get { return (int)GetValue(MyPropertyProperty2); }
set { SetValue(MyPropertyProperty2, value); }
}
public static readonly DependencyProperty MyPropertyProperty2 =
DependencyProperty.Register("MyProperty2", typeof(int), typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));
public int MyProperty3
{
get { return (int)GetValue(MyPropertyProperty3); }
set { SetValue(MyPropertyProperty3, value); }
}
public static readonly DependencyProperty MyPropertyProperty3 =
DependencyProperty.Register("MyProperty3", typeof(int), typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));
public int MyProperty4
{
get { return (int)GetValue(MyPropertyProperty4); }
set { SetValue(MyPropertyProperty4, value); }
}
public static readonly DependencyProperty MyPropertyProperty4 =
DependencyProperty.Register("MyProperty4", typeof(int), typeof(MyDependencyObject<TypeA, TypeB, TypeC, TypeD>));
}
My question is whether anybody is aware of any tricks, hacks, or legitimate solutions to reducing the number of times the full type name with generic parameters needs to be referenced in situations such as the one shown above.
Full disclosure: I did bring this up as an issue on the Microsoft .Connect site but they rejected the idea of a self referencing keyword but offered no workaround or alternative solution. My idea was to use some keyword such as Owner, OwnerClass, or ThisType in order to generically refer to the type in which the keyword is being used.