We recently upgraded to NMock2 RC2 but found a regression when mocking an interface with a generic method. Attempting to generate a mock for an interface like:

    public interface IWithGenericMethods
        T ReadVariable<T>(string name);
        void SetVariable<T>(string name, T val);

throws the following exception:

failed: System.TypeLoadException : Signature of the body and declaration in a method implementation do not match.  Type: 'MockObjectType1'.  Assembly: 'MockObjects, Version=, Culture=neutral, PublicKeyToken=null'.
    at System.Reflection.Emit.TypeBuilder._TermCreateClass(Int32 handle, Module module)
    at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
    at System.Reflection.Emit.TypeBuilder.CreateType()
    C:\Software\RIA\ThirdPartyLibraries\NMock2\nmock2\src\NMock2\Monitoring\MockObjectFactory.cs(153,0): at NMock2.Monitoring.MockObjectFactory.CreateType(String typeName, Type mockedType)
    C:\nmock2\src\NMock2\Monitoring\MockObjectFactory.cs(137,0): at NMock2.Monitoring.MockObjectFactory.GetMockedType(TypeId id1, Type mockedType)
    C:\nmock2\src\NMock2\Monitoring\MockObjectFactory.cs(120,0): at NMock2.Monitoring.MockObjectFactory.CreateMockObject(Mockery mockery, Type mockedType, String name)
    C:\nmock2\src\NMock2\Mockery.cs(53,0): at NMock2.Mockery.NewMock(Type mockedType, String name)

The exception occurs because NMock2.Monitoring.MockObjectFactory.PrepareMethodGenerator() does not define the generic parameters on the MethodBuilder for needed for generic methods. I was able to patch a local copy to correct this problem using an example from the Spring.NET project. The relevant code is on line 195-219 of
(The ReflectionUtils.GetGenericParameterNames() method referenced by that code simply returns an array of strings built from the Type.Name array passed in)

Hopefully the NMock2 source can be patched to correct this problem before the next release.

Thanks for all of the recent improvements,
    Mike Attili
    Amaxo, Inc.