Adding setter methods to TypeMerger

I recently wanted to merge two C# objects into a single object. The objects were dumb models, so I really just wanted to copy the properties and their values.

I came across Kyle Finley’s amazing TypeMerger class, which is designed for exactly this scenario – you use it as follows:

var object1 = GetSomeObject();
var object2 = GetSomeOtherObject();
var mergedObject = TypeMerger.MergeTypes(object1, object2);

It works by creating a brand new type, on the fly, and generating a backing field and a property getter for each property in the source objects – very cool!

However, it had one limitation – it only defined a getter for each property, not a setter. I wanted to URL encode all my string properties, and this wasn’t possible.

Fortunately, the code is clearly written, so it was straightforward to update it to support setters.

Firstly, I added a new unit-test so that my OODA loop would be quicker:

        public void VerifyThatNewTypesHaveSetters()
            var result = TypeMerger.MergeTypes(new { PropertyOne = "A" }, new { PropertyTwo = "B" });

            Assert.AreEqual(true, result.GetType().GetProperty("PropertyOne").CanWrite);
            Assert.AreEqual(true, result.GetType().GetProperty("PropertyTwo").CanWrite);
            PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(result);

            Assert.AreEqual("A", pdc["PropertyOne"].GetValue(result));
            Assert.AreEqual("B", pdc["PropertyTwo"].GetValue(result));

            pdc["PropertyOne"].SetValue(result, "1");
            pdc["PropertyTwo"].SetValue(result, "2");

            Assert.AreEqual("1", pdc["PropertyOne"].GetValue(result));
            Assert.AreEqual("2", pdc["PropertyTwo"].GetValue(result));

I then used Linqpad to write the class I wanted to be output, so that I could view the necessary IL.

Finally, I updated BuildProperties method in TypeMerger.cs to include the setter generation

private static void BuildProperties (TypeBuilder typeBuilder, FieldBuilder[] fields)
    // Kyle's code

    MethodBuilder setMethod = typeBuilder.DefineMethod(
        String.Format("Set_{0}", propertyName),
        MethodAttributes.Public | MethodAttributes.SpecialName,
        new Type[] { fields[i].FieldType }

    ILGenerator setMethodGenerator = setMethod.GetILGenerator();
    setMethodGenerator.Emit(OpCodes.Ldarg_0);     // load this
    setMethodGenerator.Emit(OpCodes.Ldarg_1);     // load value
    setMethodGenerator.Emit(OpCodes.Stfld, fields[i]); // store into field
    setMethodGenerator.Emit(OpCodes.Ret); // return


Et voila!


Connecting 2 1440p displays to a Retina Macbook Pro.

My boss very kindly bought me two Dell U2515H monitors (which are utterly, utterly lovely – best monitors I’ve ever had), which are 1440P monitors.

I had a few issues configuring them with a 2013 Macbook Pro Retina, so here’s the problems and how I solved them.

The first issue was that 1 monitor had a maximum resolution of 2048×1152. This was caused because I had 1 monitor plugged into HDMI (which worked fine) and the the other plugged in to a Thunderbolt port with a Thunderbolt → HDMI convertor. The fix was to use the included MiniDisplayPort → HDMI cable – plug the HDMI end into the monitor and the mini display port end into … the Thunderbolt port! Apparently, Thunderbolt is backwardly compatible with Mini Display Port. You learn something new every day ūüôā

At this point everything was perfect in Mac OS X. However, I do most of my work in a Windows 8.1 Parallels VM, and certain applications were displaying with large, fuzzy text, as though they had been independently scaled by Windows.

Which they were…

The fix was to change the Windows DPI scaling settings. Click Start and type “Display” to find the display settings dialog. Then set the slider in “Change the size of all items” to it’s lowest setting of “Smaller”.

Display settings

And I now had 2 25″ 1440P displays, which are pretty much the perfect combination of screen-size, resolution, DPI in my view.

Debugging an iPhone’s network traffic.

One of the most frustrating things when writing an iPhone app (now that we have Swift to banish the hell that is Objective-C!) is the lack of debugging tools, in particular not having Wireshark available.

Fret no more however. It turns out that you can  mirror all your iPhone network traffic to your Mac by exposing a virtual network interface.

Simply launch Terminal, and enter:

$> rvictl -s <UDID of Phone>


$> rvictl -s 4738101a4a910ffa9e368ef683fb2135dec12716

Starting device 4738101a4a910ffa9e368ef683fb2135dec12716 [SUCCEEDED] with interface rvi0

(your UDID can be determined via iTunes)

This will create a new network interface called rvi0 (or whatever the success message tells you) that you can listen on with Wireshark.

Adding a network delay in Windows Server 2008 R2

There is a very cool tool called DummyNet (aka IPFW) which allows you to add latency and set bandwidth or packet loss parameters to any network connection. This can be really useful when testing a client-server application on your local machine, or with virtual machines as it will simulate a more normal network experience and can highlight when you are taking too many round-trips to the server, or 

Download the binary (there is a link at the top of the page) and extract it (I’m assuming you put it in C:\DummyNet).

If you are using a 64-bit version of Windows, then copy all the files in \binary64 to \binary.

Follow the instructions in \binary\readme.txt.

Launch a Command Prompt (as administrator) and start slowing down your network!

Set 200ms of latency

Note we set the latency as 100ms as it will be applied to both incoming and outgoing messages (thus giving 200ms total).

$> ipfw pipe 3 config delay 100ms

$> ipfw add pipe 3 ip from any to any

Remove settings

$> ipfw -q flush

$> ipfw -q pipe flush


Disable dashboard in Mac OS X

I find the dashboard in Mac OS X pretty pointless. Luckily, there is an easy way to remove it (thanks to this post).

Note that I’ve tested this in 10.8 Mountain Lion.


defaults write mcx-disabled -boolean YES 
killall Dock


defaults write mcx-disabled -boolean NO
killall Dock

A tale of two jobs.

For the last 3 years, I have been working for a large, UK financial institution, on a major transformation programme. The aim of this programme was to rip out their 40 year old banking platform, and replace it with a brand-new SAP platform. The total cost of this project was ¬£400 million. Yet when I asked for a 2nd monitor to complement my existing 19″ (and 2 monitors is the *minimum* a developer should expect these days) I waited 6 weeks only to have a man turn up and plonk a 17″ on my desk.

“What the hell is that?!” I exclaimed.

“Yeah, sorry, budget won’t stretch to 19 inches” he explained.

I checked on Amazon and the difference in price was approximately £30. Now, bear in mind that they were paying over £500 a day for me, and you can see how nuts this was!

Before I sound like a whiny little bitch, it’s important to understand that this is an illustrative example of the attitude. The PC I was supplied with was unusably slow – it would sometimes take 15 seconds to open “All Programs” in the Start menu, and when I ordered another 2 GB of RAM a man came and installed 1…

Yet, what a contrast it is with my new company. 3 weeks before I joined they gave me my new laptop. It’s a top-spec Retina Macbook Pro that literally cost more than my car (granted my car wasn’t very expensive, but still!). And the day I started, my boss said “Here’s your iPhone 5. Oh, and someone pinched your monitor so pick whatever you want off Amazon and get Sally to order it.”. The next day I had two 24″ monitors on my desk. Also, there is free Coke in the fridge and nice toilet paper in the bogs.

The flip-side is that the work is challenging – but then, that’s why they employed me. I’m no longer a faceless cog in a giant machine – I’m once again a valued employee and I love it!

Using MvcSiteMapProvider with Twitter Bootstrap

<a href=”“>MvcSiteMapProvider</a> provides an easy way to add Sitemap functionality to your MVC application. One very nice thing is that integrating it with Twitter’s Bootstrap UI framework is child’s play (assuming the child in question has a decent background in .NET web-programming of course).

Once you have NuGet’ed both the sitemap provider and Bootstrap, and defined your sitemap XML file, you will typically add the following code to your master page to render the menu into the output HTML.


However, Bootstrap expects the HTML elements to use certain classes (e.g. “nav nav-pills”) which, by default, MvcSiteMapProvider will not do.

You can fix this easily by modifying the following template:


to include all the necessary CSS classes.

Edited template

<!-- Edited to work with Twitter Bootstrap by adding 'nav' and 'nav-pills' classes. -->
<ul class="nav nav-pills">
<% foreach (var node in Model.Nodes) { %>
    <!-- Edited to work with bootstrap by adding 'active' class for current node. -->
    <li class='<%= node.IsCurrentNode ? "active" : string.Empty %>'><%=Html.DisplayFor(m => node)%>
    <% if (node.Children.Any()) { %>
        <%=Html.DisplayFor(m => node.Children)%>
    <% } %>
<% } %>