public void GetMethodByArgumentValuesResolvesToExactMatchIfAvailable() { GetMethodByArgumentValuesTarget.DummyArgumentType[] typedArg = new GetMethodByArgumentValuesTarget.DummyArgumentType[] { }; GetMethodByArgumentValuesTarget foo = new GetMethodByArgumentValuesTarget(1, typedArg); Type type = typeof(GetMethodByArgumentValuesTarget); MethodInfo[] candidateMethods = new MethodInfo[] { type.GetMethod("MethodWithSimilarArguments", new Type[] {typeof(int), typeof(ICollection)}) , type.GetMethod("MethodWithSimilarArguments", new Type[] {typeof(int), typeof(GetMethodByArgumentValuesTarget.DummyArgumentType[])}) , type.GetMethod("MethodWithSimilarArguments", new Type[] {typeof(int), typeof(object[])}) }; // ensure noone changed our test class Assert.IsNotNull(candidateMethods[0]); Assert.IsNotNull(candidateMethods[1]); Assert.IsNotNull(candidateMethods[2]); Assert.AreEqual("ParamArrayMatch", foo.MethodWithSimilarArguments(1, new object())); Assert.AreEqual("ExactMatch", foo.MethodWithSimilarArguments(1, (GetMethodByArgumentValuesTarget.DummyArgumentType[])typedArg)); Assert.AreEqual("AssignableMatch", foo.MethodWithSimilarArguments(1, (ICollection)typedArg)); MethodInfo resolvedMethod = ReflectionUtils.GetMethodByArgumentValues(candidateMethods, new object[] { 1, typedArg }); Assert.AreSame(candidateMethods[1], resolvedMethod); }
public void GetMethodByArgumentValuesCanResolveWhenAmbiguousMatchIsOnlyDifferentiatedByParams() { GetMethodByArgumentValuesTarget.DummyArgumentType[] typedArg = new GetMethodByArgumentValuesTarget.DummyArgumentType[] { }; GetMethodByArgumentValuesTarget foo = new GetMethodByArgumentValuesTarget(1, typedArg); Type type = typeof(GetMethodByArgumentValuesTarget); MethodInfo[] candidateMethods = new MethodInfo[] { type.GetMethod("ParamOverloadedMethod", new Type[] {typeof(string), typeof(string), typeof(string)}) ,type.GetMethod("ParamOverloadedMethod", new Type[] {typeof(string), typeof(string), typeof(bool)}) ,type.GetMethod("ParamOverloadedMethod", new Type[] {typeof(string), typeof(string), typeof(string), typeof(string), typeof(object[])}) }; // ensure no one changed our test class Assert.IsNotNull(candidateMethods[0]); Assert.IsNotNull(candidateMethods[1]); Assert.IsNotNull(candidateMethods[2]); Assert.AreEqual("ThreeStringsOverload", foo.ParamOverloadedMethod(string.Empty, string.Empty, string.Empty)); Assert.AreEqual("TwoStringsAndABoolOverload", foo.ParamOverloadedMethod(string.Empty, string.Empty, default(bool))); Assert.AreEqual("FourStringsAndAParamsCollectionOverload", foo.ParamOverloadedMethod(string.Empty, string.Empty, string.Empty, string.Empty, typedArg)); MethodInfo resolvedMethod = ReflectionUtils.GetMethodByArgumentValues(candidateMethods, new object[] { string.Empty, string.Empty, string.Empty, string.Empty, typedArg }); Assert.AreSame(candidateMethods[2], resolvedMethod); }
public void GetConstructorByArgumentValuesResolvesToExactMatchIfAvailable() { GetMethodByArgumentValuesTarget.DummyArgumentType[] typedArg = new GetMethodByArgumentValuesTarget.DummyArgumentType[] { }; Type type = typeof(GetMethodByArgumentValuesTarget); ConstructorInfo[] candidateConstructors = new ConstructorInfo[] { type.GetConstructor(new Type[] {typeof(int), typeof(ICollection)}) , type.GetConstructor(new Type[] {typeof(int), typeof(GetMethodByArgumentValuesTarget.DummyArgumentType[])}) , type.GetConstructor(new Type[] {typeof(int), typeof(object[])}) }; // ensure noone changed our test class Assert.IsNotNull(candidateConstructors[0]); Assert.IsNotNull(candidateConstructors[1]); Assert.IsNotNull(candidateConstructors[2]); Assert.AreEqual("ParamArrayMatch", new GetMethodByArgumentValuesTarget(1, new object()).SelectedConstructor); Assert.AreEqual("ExactMatch", new GetMethodByArgumentValuesTarget(1, (GetMethodByArgumentValuesTarget.DummyArgumentType[])typedArg).SelectedConstructor); Assert.AreEqual("AssignableMatch", new GetMethodByArgumentValuesTarget(1, (ICollection)typedArg).SelectedConstructor); ConstructorInfo resolvedConstructor = ReflectionUtils.GetConstructorByArgumentValues(candidateConstructors, new object[] { 1, typedArg }); Assert.AreSame(candidateConstructors[1], resolvedConstructor); }
public void GetMethodByArgumentValuesMatchesNullableArgs() { var typedArg = new GetMethodByArgumentValuesTarget.DummyArgumentType[] {}; var foo = new GetMethodByArgumentValuesTarget(1, typedArg); var type = typeof (GetMethodByArgumentValuesTarget); var candidateMethods = new MethodInfo[] { type.GetMethod("MethodWithSimilarArguments", new Type[] {typeof (int), typeof (ICollection)}) , type.GetMethod("MethodWithSimilarArguments", new Type[] {typeof (int), typeof (GetMethodByArgumentValuesTarget.DummyArgumentType[])}) , type.GetMethod("MethodWithSimilarArguments", new Type[] {typeof (int), typeof (object[])}) , type.GetMethod("MethodWithNullableArgument", new Type[] {typeof (int?)}) }; // ensure noone changed our test class Assert.IsNotNull(candidateMethods[0]); Assert.IsNotNull(candidateMethods[1]); Assert.IsNotNull(candidateMethods[2]); Assert.IsNotNull(candidateMethods[3]); Assert.AreEqual("ParamArrayMatch", foo.MethodWithSimilarArguments(1, new object())); Assert.AreEqual("ExactMatch", foo.MethodWithSimilarArguments(1, (GetMethodByArgumentValuesTarget.DummyArgumentType[]) typedArg)); Assert.AreEqual("AssignableMatch", foo.MethodWithSimilarArguments(1, (ICollection) typedArg)); Assert.AreEqual("NullableArgumentMatch", foo.MethodWithNullableArgument(null)); var resolvedMethod = ReflectionUtils.GetMethodByArgumentValues(candidateMethods, new object[] {null}); Assert.AreSame(candidateMethods[3], resolvedMethod); }