From 7023a6c6045e661ea7132bb05b05b44c31398a53 Mon Sep 17 00:00:00 2001 From: Jeremy Meng Date: Sat, 25 Mar 2017 12:13:02 -0700 Subject: [PATCH] Fix bug in HasMatchingParameterTypes The bug was introduced when replacing the `Type.GetMethod(...)` call to the overload with a `Type[]` parameter since that overload is not supported in .NET CORE. The correct behavior should be checking for compatible argument types, instead of strict matching argument types. --- Source/Extensions.cs | 9 +++++++-- Source/Protected/ProtectedMock.cs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Source/Extensions.cs b/Source/Extensions.cs index 44fb2fd80..207277c31 100644 --- a/Source/Extensions.cs +++ b/Source/Extensions.cs @@ -269,7 +269,7 @@ public static TAttribute GetCustomAttribute(this ICustomAttributePro } #endif - public static bool HasMatchingParameterTypes(this MethodInfo method, Type[] paramTypes) + public static bool HasCompatibleParameterTypes(this MethodInfo method, Type[] paramTypes) { var types = method.GetParameterTypes().ToArray(); if (types.Length != paramTypes.Length) @@ -279,7 +279,12 @@ public static bool HasMatchingParameterTypes(this MethodInfo method, Type[] para for (int i = 0; i < types.Length; i++) { - if (types[i] != paramTypes[i]) + var parameterType = paramTypes[i]; + if (parameterType == typeof(object)) + { + continue; + } + else if (!types[i].IsAssignableFrom(parameterType)) { return false; } diff --git a/Source/Protected/ProtectedMock.cs b/Source/Protected/ProtectedMock.cs index 33f50922c..2c22661d5 100644 --- a/Source/Protected/ProtectedMock.cs +++ b/Source/Protected/ProtectedMock.cs @@ -192,7 +192,7 @@ private static MethodInfo GetMethod(string methodName, params object[] args) { var argTypes = ToArgTypes(args); return typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) - .SingleOrDefault(m => m.Name == methodName && m.HasMatchingParameterTypes(argTypes)); + .SingleOrDefault(m => m.Name == methodName && m.HasCompatibleParameterTypes(argTypes)); } private static Expression> GetMethodCall(MethodInfo method, object[] args)