John’s Adventures

Archive for July 2006

An Intelligent Copy Constructor In C# Using Reflection

Whenever I write a copy constructor I know that at some point I'm going to add a field to the class, forget to add it to the copy constructor and cause a difficult-to-identify bug later on down the line. So I came up with a simple solution in C# to this problem using Reflection. Here's the code (oh, you'll need to put 'Using System.Reflection;' at the top of the file):

    public MyClass( MyClass rhs )
    {
        // get all the fields in the class
        FieldInfo[] fields_of_class = this.GetType().GetFields(
          BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance );

        // copy each value over to 'this'
        foreach( FieldInfo fi in fields_of_class )
        {
            fi.SetValue( this, fi.GetValue( rhs ) );
        }
    }

Basically, it enumerates all the public and non-public fields in the class and copies their values from the class passed in. You can modify the flags passed to GetFields() to include or exclude fields as you need. So now I can't miss out any fields that I add to the class - and that means at least one less bug I've caused!

Clarification: This sample only performs a shallow copy of MyClass and therefore if you have fields that are reference types (like classes), then it will only copy a reference to those fields from 'rhs'. The class I used this for only contained value types (like int's and so forth) and so works exactly as I'd expect. I'll leave the modifications to this code to handle reference types as an exercise for the reader!

Not This Time England

Having just watched England go out of the World Cup and having realised about half way through the match that I wanted them to win - I spent the game on the edge of my seat and have ended up, like most England fans, frustrated and disappointed.

The thing that's impressed me the most from watching their games is how Eriksson has managed to take a team littered with match-winning world class players who perform to the highest levels each week for their clubs - and make them play like a second division team with no confidence and a defensive, losing mentality.

You've got to wonder how a guy can get paid several million pounds a year and so clearly get it wrong. Play people out of position. Make them play in a formation they're not comfortable in. Make all the wrong decisions about substitutions. It's just a shame his footballing know-how and motivational skills don't match up to his powers of salary negotiation!

You could blame the players saying they're better than that. But as a player you don't go out on the field and do your own thing, you play in the way the manager tells you to, you play in a position and have a specific job to do. If you think you can do better doing something else then tough, you do what the manager tells you to.

Of course I should have known they'd go out. My two tips for the tournament as I mentioned before - Spain and Argentina - went out after playing so well in the group stages. So as soon as I actually wanted England to win they were doomed to failure. Sorry about that England…