• Implied generic parameter types

    I’m not sure of the exact name of this feature, but I stumbled across it today. If a generic method uses the type for a parameter, the complier assumes the generic type, which makes for cleaner code.

    using System;

    class Program
    static void Main()
    MyClass obj = new MyClass();

    public class MyClass
    public static void WriteInputStatic<T>(T input) { Console.WriteLine(input); }
    public void WriteInput<T>(T input) { Console.WriteLine(input); }

    I can’t think of many use cases, but kind of a cool feature anyway. I was using it to serialize and deserialize a type for testing the correct serialization/deserialization.

    internal static T Serialize<T>(T worker) where T : class
        MemoryStream serializationStream = new MemoryStream();
    new BinaryFormatter().Serialize(serializationStream, worker);
    byte[] bytes = serializationStream.ToArray();

    MemoryStream deserializationStream = new MemoryStream(bytes);
    object obj = new BinaryFormatter().Deserialize(deserializationStream);
    return obj as T;
  • Instant wiki with rBuilder MediaWiki Appliance

    The MediaWiki Appliance is

    An appliance that bundles up PHP, MySQL, Apache and MediaWiki to provide a self-contained, turn-key Wiki appliance.</p>


    Setup was easy and quick and it runs in Virtual Server and Virtual PC 2007.

  • Overriding component parameters when using Castle.MicroKernel

    Overriding component parameters when using the Castle.MicorKernel turned out to be kinda weird. Anyway, here’s one way to do it.

    typeof (ComponentA),
    new ComponentA("Customer parameter for testing."));

    I needed to override the parameter value of ComponentA for some unit tests. The parameter is normally set in the configuration file. After trying a few different approaches, I ended up with the code above. It’s more of an end to end unit test, and as such ComponentA will be resolved as a result of another Component being resolved.

  • Unit Test Live Templates for ReSharper

    Attached are Live templates that can be imported into ReSharper to help with writing unit tests. There are templates for Test, Setup, TearDown, TestFixtureSetUp and TestFixtureTearDown methods.

    I find that failing test cases are really good reminders of things that need to get done, so often I will add test methods with Assert.Fail(); as the body, usually the method name is enough detail for me to remember what needs to be done.

    Lastly, I’ve started using the ConditionalAttribute on my test class methods so they won’t be compiled in release mode. If this is not your style you may want to modify the templates.

    ReSharperUnitTestLiveTemplates.xml (2.63 KB)

    ** I’ve decided against using the ConditionalAttribute in favor of adding compiler directives to the test files. I quickly found that not using the attribute became a source for failing tests that shouldn’t have been run and/or simple code leaks. I know that many people prefer a different class library for tests, but I think that only makes sense for non unit tests. Anyway, here’s the updated file.

    ReSharperUnitTestLiveTemplates1.xml (2.48 KB)

  • Setting up VS 2005 for nant build files

    To setup nant intellisense in VS 2005 you can copy the schema from the install to the Visual Studio Install\xml\schema directory, however this won’t get all available tasks. As such, I prefer to create a nant file which calls the nantschema task (http://nant.sourceforge.net/release/0.85/help/tasks/nantschema.html). This task requires an output attribute be set, which I just set to my Visual Studio Install\xml\schema directory. Below is the build script I use:

    <?xml version="1.0" encoding="utf-8"?>
    <project name="Build nant schema to VS 2005" default="build" basedir=".">
        <target name="build">
    output="C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas\NAnt.xsd"
    target-ns="http://tempuri.org/nant-donotuse.xsd" /> </target> </project>

    You can run this after updating Nant or adding contrib projects to your installation and get the latest tasks.

    Finally, in your other build files add the xmlns attribute to the project element pointing to the name space you used as the target-ns, http://tempuri.org/nant-donotuse.xsd in the above example. You may also need to tell VS to open .build files with the xml editor, you can do this by right clicking on the .build file and choosing ‘open with’.

  • Sum a column in a tab-delimited file using Powershell

    Assuming you have a bunch of tab delimited files w/out headers and the second column as a number, you could use the following powershell command to sum the values and output the result.

    PS > $sum=0; ls | cat | %{ $.split(“`t”)[1] } | %{ $sum+=[int]$ }; $sum

    Sample File:

    a-count    7
    b-count 233
    c-count 32

  • Reflection.Emit.DeclareLocal and Reflection.Emit.DefineLabel

    Working with Reflection.Emit today I ran into two thing that I couldn’t

    quickly find answers to. First, was regarding the br.s (OpCodes.Br_S)

    code. Secondly, was regarding an error ‘Common Language Runtime

    detected an invalid program’ which didn’t provide much help and the

    cause was not obvious. After a lot of research and trial and error I

    found some answers.

    Regarding the first issue, I found this page on MSDN which also show’s

    how to create a switch statement.

    http://msdn2.microsoft.com/en-us/library/csx7wsz2(vs.80).aspx. The

    solution is to create a Label object and MakeLabel method when Emitting

    the IL.

    Label endOfMethod = il.DefineLabel();
    il.Emit(OpCodes.Br_S, endOfMethod);

    The above will create IL like this:

    IL_0017:  br.s       IL_001
    IL_0019:  ldloc.1

    The second problem maybe should have been obvious, but most of what I
    read didn’t cover this. If you need to store something you need to
    create local storage before trying to store objects there. Not doing
    this will result in a System.InvalidProgramException. You need to
    create the storage with the il.DeclareLocal method passing in the Type
    of the object you will store there.


    The above will create IL like:

    .locals init (class [mscorlib]System.Text.StringBuilder V_0,
                string V_1)

    Working sample command line application (.net2.0) flie – Program.cs.txt (2.45 KB)