ObjectListView Update (220.127.116.11): Debugger Visualizer
Posted on March 11, 2007
Note: You can download the complete implementation here.
For those of you just tuning in, ObjectListView is my answer to the .NET need for a DataView-like construct for ordinary objects. With ObjectListView, you can have a sorted and filtered view of any IList or IList. You can bind this view to controls like the DataGridView.
This new version adds a debugger visualizer and includes a few other small enhancements and bug fixes.
Ecki found a bug in the Master/Details demo where I was doing an invalid cast in the New button click handler. I also found that .NET 1.1 style property change events weren’t wired properly for items already in the list at the time an ObjectListView was constructed. Those two are fixed.
Boris reported that changing a list item property while iterating over ObjectListView with a foreach loop would cause an InvalidOperationException to be thrown. Indeed! This is by design; any change to a .NET collection must invalidate all enumerators of the collection. ObjectListView is really a view over a collection, though, and not a collection itself. This view presents the appearance of a collection. This virtual collection does change when items are added, removed, or replaced in the underlying collection. When the value of a list item property changes, however, the virtual collection only changes in certain circumstances. Fortunately, these circumstances are well-understood: if the property is not included in either the ObjectListView sort or filter, the virtual collection does not change. In this case, there is no need to invalidate any enumeration in progress. In the updated version, ObjectListView allows enumeration to continue following a list item property change if the property does not belong to the sort or the filter criteria.
I added a ToArray() method, as I find it irritating to have to declare an array and copy into it with CopyTo() in two steps. By request, OnListChanged(), OnSorted() and OnAddingNew() are now protected virtual, for extensibility. These methods follow the usual convention of event-raising methods in .NET. If you override one of them, be sure to call the base version so that the event will in fact be raised.
Since more than a few have asked, I’ve added the license terms to the Readme.txt file of the download. ObjectListView is free – no strings attached.
This is the big cool addition in my estimation. If you’re debugging a program that uses ObjectListView, you can see a nice dialog that shows you the current state of the ObjectListView and it’s underlying list. At a breakpoint, move the mouse over the variable that is the ObjectListView, and click the little magnifying glass. You’ll see something like this:
On the left side, you’ll see information about the type of objects in the underlying list (“List Item Type”), and below that, information about the list itself (“List Type”). If the ObjectListView is sorted, the sort properties and sort direction of each are listed. Below that is the current filter expression, if any. If you’re using a filter predicate (as I am in the example), it will tell you that.
The right side shows two tabs. The one on the top (“View”) shows the list items that are currently exposed by the ObjectListView, in the order defined by the current sort, and excluding any items that are filtered out. This is exactly what you would see in a foreach loop, enumerating the items in the ObjectListView. The second tab (“List”) shows all of the list items in the underlying list, in the order in which they appear in the list. Cool, huh?
But wait, there’s more! If you’re having problems getting ObjectListView to work the way that you expect it to, check out the Analysis button in the lower left. If you click that, you’ll see a synopsis of how the list and list item types work with ObjectListView. It will tell you about any potential problems, and recommend a solution. Here’s what it looks like:
I hope that the visualizer is helpful. Debug visualizers are really not very hard to write, and I encourage you to write your own for any complex types that you’re coding up. Take a look at the source in ObjectListViewVisualizer.cs to see how to do it. The only significant constraint on your code is that the type you’re visualizing needs to be serializable.
Enjoy – and please don’t hesitate to speak up if you have questions or thoughts about ObjectListView!