Just a small idea before I go to work. As follow up to my yesterdays post about Rhino.Mocks. I figured, that instead of supplying arrays of object for constructor parameters, which is error prone and doesn't give you the safety of compiler checks, we can actually insert the call to constructor using Expressions (notice that this is solution that will work only in .NET >= 3.5).
Here's quick and dirty sketch of remade method:
public TTypeToMock Mock<TTypeToMock>(Kind mockKind, Expression<Func<TTypeToMock>> ctorCall, params Type[] extraTypes)
{
if (extraTypes == null) throw new ArgumentNullException("extraTypes");
if (ctorCall == null) throw new ArgumentNullException("ctorCall");
if (mockKind == Kind.WithRemoting)
mockKind &= Kind.Strict;
else if (!_mockMethods.ContainsKey(mockKind))
{
if ((mockKind & Kind.WithRemoting) != 0)
throw new ArgumentException("This kind of mock does not support remoting.");
throw new ArgumentException("Invalid mock kind.", "mockKind");
}
var newCall = ctorCall.Body as NewExpression;
if (newCall == null)
throw new ArgumentException("Not a constructor call.", "ctorCall");
var ctorArgs = new object[newCall.Arguments.Count];
for (int i = 0; i < ctorArgs.Length; i++)
{
var param = newCall.Arguments[i] as ConstantExpression;
if (param == null)
throw new ArgumentException("You may only place constant values in calls to the constructor.");
ctorArgs[i] = param.Value;
}
return CreateMock<TTypeToMock>(mockKind, extraTypes, ctorArgs);
}
private TTypeToMock CreateMock<TTypeToMock>(Kind mockKind, Type[] extraTypes, object[] ctorArgs)
{
//NOTE: possibly with lock in this call
var mock = (TTypeToMock) _mockMethods[mockKind](_repo, typeof (TTypeToMock), ctorArgs, extraTypes);
return mock;
}
And with that you get in return this:
Notice that you no longer need to specify <MyClass> (it's grayed out) since compiler can infer that from the Expression.
Camel humps intellisense. Start using it, and you'll be wondering how you could ever have lived without it.
The title says it all. I've uninstalled beta2, and currently I'm trying to install the final version of Visual Studio 2008.