fix for sprnet-1107, sprnet-1111
This commit is contained in:
@@ -136,8 +136,8 @@ namespace Spring.Collections.Generic
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="TValue"/> with the specified key. Set
|
||||
/// will throw an exception
|
||||
/// Gets the value with the specified key.
|
||||
/// Set will throw an exception
|
||||
/// </summary>
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
|
||||
@@ -129,15 +129,16 @@ namespace Spring.Reflection.Dynamic
|
||||
/// A method return value.
|
||||
/// </returns>
|
||||
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
|
||||
/// <summary>
|
||||
/// Factory class for dynamic methods.
|
||||
/// </summary>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
public class DynamicMethod : BaseDynamicMember
|
||||
{
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
public class DynamicMethod : BaseDynamicMember
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates dynamic method instance for the specified <see cref="MethodInfo"/>.
|
||||
/// </summary>
|
||||
@@ -233,6 +234,33 @@ namespace Spring.Reflection.Dynamic
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user