diff --git a/src/Spring/Spring.Core/Util/ReflectionUtils.cs b/src/Spring/Spring.Core/Util/ReflectionUtils.cs index 9bfece08..213356da 100644 --- a/src/Spring/Spring.Core/Util/ReflectionUtils.cs +++ b/src/Spring/Spring.Core/Util/ReflectionUtils.cs @@ -231,7 +231,7 @@ namespace Spring.Util ParameterInfo[] parameters = m.GetParameters(); bool isMatch = true; bool isExactMatch = true; - object[] paramValues = argValues; + object[] paramValues = (argValues==null)?new object[0] : argValues; try { @@ -246,19 +246,26 @@ namespace Spring.Util } } - for (int i = 0; i < parameters.Length; i++) + if (parameters.Length != paramValues.Length) + { + isMatch = false; + } + else { - Type paramType = parameters[i].ParameterType; - object paramValue = paramValues[i]; - if ((paramValue == null && paramType.IsValueType) - || (paramValue != null && !paramType.IsAssignableFrom(paramValue.GetType()))) + for (int i = 0; i < parameters.Length; i++) { - isMatch = false; - break; - } - if (paramValue == null || paramType != paramValue.GetType()) - { - isExactMatch = false; + Type paramType = parameters[i].ParameterType; + object paramValue = paramValues[i]; + if ((paramValue == null && paramType.IsValueType) + || (paramValue != null && !paramType.IsAssignableFrom(paramValue.GetType()))) + { + isMatch = false; + break; + } + if (paramValue == null || paramType != paramValue.GetType()) + { + isExactMatch = false; + } } } } diff --git a/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs b/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs index 89010ad3..f039fe8a 100644 --- a/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs +++ b/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs @@ -48,6 +48,69 @@ namespace Spring.Util [TestFixture] public sealed class ReflectionUtilsTests { + #region Helper class for http://jira.springframework.org/browse/SPRNET-992 tests + + public class Foo + { + public readonly string a = ""; + public readonly int b = -1; + public readonly char c = '0'; + + public Foo(string a, int b, char c) + { + this.a = a; + this.b = b; + this.c = c; + } + + public Foo(string a) + { + this.a = a; + } + + public Foo() + { + } + } + + #endregion + + [Test(Description="http://jira.springframework.org/browse/SPRNET-992")] + public void ShouldPickDefaultConstructorWithoutArgs() + { + object[] args = new object[] {}; + ConstructorInfo best = ReflectionUtils.GetConstructorByArgumentValues(typeof (Foo).GetConstructors(), null); + Foo foo = (Foo) best.Invoke(args); + + Assert.AreEqual("", foo.a); + Assert.AreEqual(-1, foo.b); + Assert.AreEqual('0', foo.c); + } + + [Test(Description="http://jira.springframework.org/browse/SPRNET-992")] + public void ShouldPickDefaultConstructor() + { + object[] args = new object[] {}; + ConstructorInfo best = ReflectionUtils.GetConstructorByArgumentValues(typeof (Foo).GetConstructors(), args); + Foo foo = (Foo) best.Invoke(args); + + Assert.AreEqual("", foo.a); + Assert.AreEqual(-1, foo.b); + Assert.AreEqual('0', foo.c); + } + + [Test(Description="http://jira.springframework.org/browse/SPRNET-992")] + public void ShouldPickSingleArgConstructor() + { + object[] args = new object[] { "b"}; + ConstructorInfo best = ReflectionUtils.GetConstructorByArgumentValues(typeof(Foo).GetConstructors(), args); + Foo foo = (Foo)best.Invoke(args); + + Assert.AreEqual("b", foo.a); + Assert.AreEqual(-1, foo.b); + Assert.AreEqual('0', foo.c); + } + [Test] public void GetParameterTypes() {