private void AssertTypesMatches(object value) { string type = null; Type returnType = method.ReturnType; if (value == null) { type = "null"; if (returnType.IsValueType == false) { return; } if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Nullable <>)) { return; } } else { //we reduce checking of generic types because of the complexity, //we let the runtime catch those mistakes returnType = GenericsUtil.GetRealType(returnType, originalInvocation); Type valueType = value.GetType(); if (returnType.IsInstanceOfType(value)) { return; } type = valueType.FullName; } throw new InvalidOperationException(string.Format("Type '{0}' doesn't match the return type '{1}' for method '{2}'", type, returnType, MethodCallUtil.StringPresentation(Originalinvocation, method, new object[0]))); }
private void AssertReturnTypeMatch(Delegate value) { if (GenericsUtil.GetRealType(method.ReturnType, Originalinvocation) .IsAssignableFrom(value.Method.ReturnType) == false) { throw new InvalidOperationException("The delegate return value should be assignable from " + method.ReturnType.FullName); } }
/// <summary> /// Asserts that the delegate has the same parameters as the expectation's method call /// </summary> protected void AssertDelegateArgumentsMatchMethod(Delegate callback) { ParameterInfo[] callbackParams = callback.Method.GetParameters(), methodParams = method.GetParameters(); string argsDontMatch = "Callback arguments didn't match the method arguments"; if (callbackParams.Length != methodParams.Length) { throw new InvalidOperationException(argsDontMatch); } for (int i = 0; i < methodParams.Length; i++) { Type methodParameter = GenericsUtil.GetRealType(methodParams[i].ParameterType, Originalinvocation); if (methodParameter != callbackParams[i].ParameterType) { throw new InvalidOperationException(argsDontMatch); } } }