From 59c13f18ec7b4fd9dcecec6979952010f8690b69 Mon Sep 17 00:00:00 2001 From: eeichinger Date: Fri, 21 Nov 2008 02:40:47 +0000 Subject: [PATCH] fix for sprnet-1107, sprnet-1111 --- .../Collections/Generic/ReadOnlyDictionary.cs | 4 +- .../Reflection/Dynamic/DynamicMethod.cs | 54 ++++++++++++++----- .../Reflection/Dynamic/DynamicMethodTests.cs | 12 +++-- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/Spring/Spring.Core/Collections/Generic/ReadOnlyDictionary.cs b/src/Spring/Spring.Core/Collections/Generic/ReadOnlyDictionary.cs index 359aaec5..9b3858a4 100644 --- a/src/Spring/Spring.Core/Collections/Generic/ReadOnlyDictionary.cs +++ b/src/Spring/Spring.Core/Collections/Generic/ReadOnlyDictionary.cs @@ -136,8 +136,8 @@ namespace Spring.Collections.Generic } /// - /// Gets the with the specified key. Set - /// will throw an exception + /// Gets the value with the specified key. + /// Set will throw an exception /// public TValue this[TKey key] { diff --git a/src/Spring/Spring.Core/Reflection/Dynamic/DynamicMethod.cs b/src/Spring/Spring.Core/Reflection/Dynamic/DynamicMethod.cs index 230bc304..221dc15d 100644 --- a/src/Spring/Spring.Core/Reflection/Dynamic/DynamicMethod.cs +++ b/src/Spring/Spring.Core/Reflection/Dynamic/DynamicMethod.cs @@ -129,15 +129,16 @@ namespace Spring.Reflection.Dynamic /// A method return value. /// public object Invoke(object target, params object[] arguments) - { - // special case - when calling Invoke(null,null) it is undecidible if the second null is an argument or the argument array - if (arguments==null && nullArguments.Length==1) arguments=nullArguments; - AssertUtils.IsTrue( - nullArguments.Length == (arguments==null?0:arguments.Length) - , string.Format("Invalid number of arguments passed into method {0} - expected {1}, but was {2}", methodInfo.Name, nullArguments.Length, arguments.Length) - ); - - return this.method(target, arguments); + { + // special case - when calling Invoke(null,null) it is undecidible if the second null is an argument or the argument array + if (arguments==null && nullArguments.Length==1) arguments=nullArguments; + int arglen = (arguments==null?0:arguments.Length); + AssertUtils.IsTrue( + nullArguments.Length == arglen + , string.Format("Invalid number of arguments passed into method {0} - expected {1}, but was {2}", methodInfo.Name, nullArguments.Length, arglen) + ); + + return this.method(target, arguments); } #else private IDynamicMethod dynamicMethod; @@ -210,9 +211,9 @@ namespace Spring.Reflection.Dynamic /// /// Factory class for dynamic methods. /// - /// Aleksandar Seovic - public class DynamicMethod : BaseDynamicMember - { + /// Aleksandar Seovic + public class DynamicMethod : BaseDynamicMember + { /// /// Creates dynamic method instance for the specified . /// @@ -233,6 +234,33 @@ namespace Spring.Reflection.Dynamic /// Aleksandar Seovic public class DynamicMethod : BaseDynamicMember { + private class DynamicMethodImpl : IDynamicMethod + { + private readonly object[] nullArguments; + private readonly MethodInfo methodInfo; + private readonly IDynamicMethod method; + + public DynamicMethodImpl(MethodInfo methodInfo, IDynamicMethod generatedMethod) + { + this.nullArguments = new object[methodInfo.GetParameters().Length]; + this.methodInfo = methodInfo; + this.method = generatedMethod; + } + + public object Invoke(object target, params object[] arguments) + { + // special case - when calling Invoke(null,null) it is undecidible if the second null is an argument or the argument array + if (arguments==null && nullArguments.Length==1) arguments=nullArguments; + int arglen = (arguments==null?0:arguments.Length); + AssertUtils.IsTrue( + nullArguments.Length == arglen + , string.Format("Invalid number of arguments passed into method {0} - expected {1}, but was {2}", methodInfo.Name, nullArguments.Length, arglen) + ); + + return this.method.Invoke(target, arguments); + } + } + private static readonly CreateMethodCallback s_createMethodCallback = new CreateMethodCallback(CreateInternal); #region Create Method @@ -247,7 +275,7 @@ namespace Spring.Reflection.Dynamic AssertUtils.ArgumentNotNull(method, "You cannot create a dynamic method for a null value."); IDynamicMethod dynamicMethod = DynamicReflectionManager.GetDynamicMethod(method, s_createMethodCallback); - return dynamicMethod; + return new DynamicMethodImpl(method, dynamicMethod); } private static IDynamicMethod CreateInternal(MethodInfo method) diff --git a/test/Spring/Spring.Core.Tests/Reflection/Dynamic/DynamicMethodTests.cs b/test/Spring/Spring.Core.Tests/Reflection/Dynamic/DynamicMethodTests.cs index 1e52a4d2..348b80ca 100644 --- a/test/Spring/Spring.Core.Tests/Reflection/Dynamic/DynamicMethodTests.cs +++ b/test/Spring/Spring.Core.Tests/Reflection/Dynamic/DynamicMethodTests.cs @@ -121,7 +121,7 @@ namespace Spring.Reflection.Dynamic [Test] public void PassInvalidNumberOfArguments() { - IDynamicMethod dm = DynamicMethod.Create(typeof(TestMethods).GetMethod("PassNullableArgumentStatic")); + IDynamicMethod dm = DynamicMethod.Create(typeof(TestMethods).GetMethod("PassReferenceArgumentStatic")); DateTime dt = DateTime.Now; Assert.IsNull(dm.Invoke(null, null)); // this is ok @@ -259,9 +259,13 @@ namespace Spring.Reflection.Dynamic #region Helper Classes -#if NET_2_0 - class TestMethods + public class TestMethods { + public static object PassReferenceArgumentStatic( object arg ) + { + return arg; + } +#if NET_2_0 public static object Invoke( object target, object[] args ) { return PassNullableArgumentStatic( (DateTime?)(args[0]) ); @@ -276,8 +280,8 @@ namespace Spring.Reflection.Dynamic { return arg; } - } #endif + } #endregion }