Tuesday, 25 January 2011

Star wars episode IV A new hope in ASCII

If you want to see Star Wars created completely from text try the following. You will need internet access!

  • Click on start
  • Click "Run"
  • Type "CMD"
  • In the command prompt window type "telnet" and press enter
  • then type "o" and press enter
  • Now type "towel.blinkenlights.nl" And press enter
  • Now wait a few seconds and it will start Star wars episode IV A new hope

Wednesday, 12 January 2011

How to Copy and Object with Reflection

If you need to copy an object then one way of doing it is with reflection. This is useful if a new property is added to a class and you dont want to remember to update the copy function with the new field.

Calling private Methods and Properties

If you are using a 3rd party API and need to call a private method, or change the value of a private member variable then this is possible using Reflection in the System.Reflection Namespace.

Example Below

Friday, 7 January 2011

Getting performance data from SQL server

If you need to tweak a query to run a bit faster you can tell SQL server to also show performance data with the following command:


This will show each part of the query and how long it has taken etc.

To turn this off again use:


Thursday, 6 January 2011

Implementing ICustomTypeDescriptor for dynamic properties

I have recently had a requirement to bind a grid view to a record object that could have any number of properties that can be added and removed at runtime. This was to allow a user to add a new column to a result set to enter an additional set of data.

This can be achieved by having each data 'row' as a dictionary with the key being the property name and the value being a string or a class that can store the value of the property for the specified row. Of course having a List of Dictionary objects will not be able to be bound to a grid. This is where the ICustomTypeDescriptor comes in.

By creating a wrapper class for the Dictionary and making it adhere to the ICustomTypeDescriptor interface the behaviour for returning properties for an object can be overridden.

Take a look at the implementation of the data 'row' class below:

Note: In the GetProperties method I Could Cache the PropertyDescriptors once read for performance but as I'm adding and removing columns at runtime I always want them rebuilt

You will also notice in the GetProperties method that the Property Descriptors added for the dictionary entries are of type TestResultPropertyDescriptor. This is a custom Property Descriptor class that manages how properties are set and retrieved. Take a look at the implementation below:

The main properties to look at on this class are GetValue and SetValue. Here you can see the component being casted as a dictionary and the value of the key inside it being Set or retrieved. Its important that the dictionary in this class is the same type in the Row wrapper class otherwise the cast will fail. When the descriptor is created the key (property name) is passed in and is used to query the dictionary to get the correct value.