Thursday, January 20, 2011

Frustration with idiotic .NET Generics

Do you know ObservableCollection(of Object) != ObservableCollection(of ClassA)? Of course you do. But what do you do when you have a generic collection class that you want to generalize (pardon the tone twister)? You need to do a little converter function. However, if .NET can say ObservableCollection(of ClassA) isa ObservableCollection(of Object), life will be a lot easier.
Another thing it will fix is the need to create a non-generic class as the parent type of the generic class in a class hierarchy (to ease the pain of multiple "isa" call in trying to assert the subclass' type). So you can say:
If typeof o Is GenericClass(of Object) Then
instead of
If typeof o Is GenericClassUselessParent Then
to simplify the class hierarchy.

One thing hard learnt regarding Dependency Properties

I was scratching my head like crazy on this one but could not figure it out. It turns out that: if you put a default value of reference type in the Register function of DependencyProperty, this vaue will be shared by ALL instances of the property and cause problems. For example, if you have a property of the type Collection, then all data added to that collection will "magically" show up in other instances too! This has bothered me for a long time and I finally found a good explaination at this web page.