Rhino Mocks and multidimensional arrays

From the useless piece of trivia department, I just found out something very strange when trying to mock an interface with a multidimensional array property with Rhino Mocks (well, more specifically, using Castle’s DynamicProxy to generate classes from this interface). Take this mild-mannered, unassuming interface for example:

public interface IHazDoubleArray {
 double[] IHazArray { get; set; }
 double[][] IHazJaggedArray { get; set; }
 double[,] IHazArrayOfDoubleArrays { get; set; }
}

Now let’s try and mock it:

[Fact]
public void Mock_it() {
 MockRepository mocks = new MockRepository();
 mocks.DynamicMock<IHazDoubleArray>(); // <<< Doesn't work
}

/* Gives runtime error:

System.TypeLoadException : Method 'get_IHazArrayOfDoubleArrays' in type 'IHazDoubleArrayProxy2bd67b685aca41fdb90f81b9aed6189c' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.

*/

Apparently this is due to a bug in the CLR (or a bug in the BCL, Reflection.Emit?). The standard and jagged arrays (array[] and array[][]) work fine, it is only the multidimensional ones (array[,]) that trip things up (see C# Array guidelines for a refresher). Rhino Mocks 3.4 introduced remoting mocks to workaround this and other issues:

[Fact]
public void Mock_with_remoting() {
 MockRepository mocks = new MockRepository();
 mocks.DynamicMockWithRemoting<IHazDoubleArray>(); //Hooray! Works!
}

This obscure post is to improve my odds of remembering this in 18 months time when this bites me again. :)

Comments