diff --git a/src/Spring/Spring.Core/Util/ReflectionUtils.cs b/src/Spring/Spring.Core/Util/ReflectionUtils.cs index c8888aa5..d41b5490 100644 --- a/src/Spring/Spring.Core/Util/ReflectionUtils.cs +++ b/src/Spring/Spring.Core/Util/ReflectionUtils.cs @@ -24,6 +24,7 @@ using System; using System.Collections; #if NET_2_0 using System.Collections.ObjectModel; +using System.Collections.Generic; #endif using System.Globalization; using System.Reflection; @@ -32,6 +33,7 @@ using System.Security; using System.Security.Permissions; using System.Text; using System.Runtime.CompilerServices; +using System.Diagnostics; #endregion @@ -935,7 +937,7 @@ namespace Spring.Util for (int i = 0; i < attributeData.ConstructorArguments.Count; i++) { parameterTypes[i] = attributeData.ConstructorArguments[i].ArgumentType; - parameterValues[i] = ConvertValueIfNecessary(attributeData.ConstructorArguments[i].Value); + parameterValues[i] = ConvertConstructorArgsToObjectArrayIfNecessary(attributeData.ConstructorArguments[i].Value); } Type attributeType = attributeData.Constructor.DeclaringType; @@ -968,7 +970,7 @@ namespace Spring.Util if (attributeProperties[j].Name == namedArgument.MemberInfo.Name) { propertiesToSet.Add(attributeProperties[j]); - namedParameterValues.Add(ConvertValueIfNecessary(namedArgument.TypedValue.Value)); + namedParameterValues.Add(ConvertConstructorArgsToObjectArrayIfNecessary(namedArgument.TypedValue.Value)); break; } else @@ -995,7 +997,7 @@ namespace Spring.Util if (attributeFields[j].Name == namedArgument.MemberInfo.Name) { fieldsToSet.Add(attributeFields[j]); - namedFieldValues.Add(ConvertValueIfNecessary(namedArgument.TypedValue.Value)); + namedFieldValues.Add(ConvertConstructorArgsToObjectArrayIfNecessary(namedArgument.TypedValue.Value)); break; } else @@ -1043,39 +1045,26 @@ namespace Spring.Util } - private static object ConvertValueIfNecessary(object value) + private static object ConvertConstructorArgsToObjectArrayIfNecessary(object value) { - if (value == null) + if (value == null) return value; - // We are only hunting for the case of the ReadOnlyCollection here. - ReadOnlyCollection sourceArray = - value as ReadOnlyCollection; + IList constructorArguments = value as IList; - if (sourceArray == null) + if (constructorArguments == null) return value; - Type underlyingType = null; // type to be used for arguments - Array returnArray = null; - for (int i = 0; i < sourceArray.Count; i++) + var arguments = new object[constructorArguments.Count]; + + for (int i = 0; i < constructorArguments.Count; i++) { - if (underlyingType == null) - { - underlyingType = sourceArray[i].ArgumentType; - returnArray = Array.CreateInstance(underlyingType, sourceArray.Count); - } - if (!underlyingType.Equals(sourceArray[i].ArgumentType)) - { - throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, - "Types for the same named parameter of array type are expected to be same")); - } - - returnArray.SetValue(sourceArray[i].Value, i); + arguments[i] = constructorArguments[i].Value; } - return returnArray; - + return arguments; } + #endif /// diff --git a/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs b/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs index 70e8cc5a..c107b96f 100644 --- a/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs +++ b/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs @@ -94,7 +94,8 @@ namespace Spring.Util private class DummyException : ApplicationException { - public DummyException() : base("dummy message") + public DummyException() + : base("dummy message") { } } @@ -125,23 +126,23 @@ namespace Spring.Util mi.Invoke(null, null); Assert.Fail(); } - catch(TargetInvocationException tie) + catch (TargetInvocationException tie) { -// Console.WriteLine(tie); + // Console.WriteLine(tie); throw ReflectionUtils.UnwrapTargetInvocationException(tie); } Assert.Fail(); } catch (DummyException e) { -// Console.WriteLine(e); + // Console.WriteLine(e); string[] stackFrames = e.StackTrace.Split('\n'); #if !MONO // TODO: mono includes the invoke() call in inner stackframe does not include the outer stackframes - either remove or document it string firstFrameMethodName = mi.DeclaringType.FullName + "." + mi.Name; - AssertStringContains( firstFrameMethodName, stackFrames[0]); + AssertStringContains(firstFrameMethodName, stackFrames[0]); string lastFrameMethodName = MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + MethodBase.GetCurrentMethod().Name; - AssertStringContains(lastFrameMethodName, stackFrames[stackFrames.Length-1]); + AssertStringContains(lastFrameMethodName, stackFrames[stackFrames.Length - 1]); #endif } @@ -163,7 +164,7 @@ namespace Spring.Util try { - method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary( null, typeof( MapsInterfaceMethodClass ) ); + method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary(null, typeof(MapsInterfaceMethodClass)); Assert.Fail(); } catch (ArgumentNullException ex) @@ -173,7 +174,7 @@ namespace Spring.Util try { - method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary( (MethodInfo)MethodInfo.GetCurrentMethod(), null ); + method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary((MethodInfo)MethodInfo.GetCurrentMethod(), null); Assert.Fail(); } catch (ArgumentNullException ex) @@ -184,7 +185,7 @@ namespace Spring.Util try { // unrelated types - method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary( (MethodInfo)MethodInfo.GetCurrentMethod(), typeof( MapsInterfaceMethodClass ) ); + method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary((MethodInfo)MethodInfo.GetCurrentMethod(), typeof(MapsInterfaceMethodClass)); Assert.Fail(); } catch (ArgumentException ex) @@ -192,14 +193,14 @@ namespace Spring.Util Assert.AreEqual("methodInfo and implementingType are unrelated", ex.Message); } - method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary( typeof( MapsInterfaceMethodClass ).GetMethod( "SomeMethodA" ), typeof( MapsInterfaceMethodClass ) ); - Assert.AreSame( instance.MethodAInfo, method ); - method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary( typeof( IMapsInterfaceMethodInterface ).GetMethod( "SomeMethodA" ), typeof( MapsInterfaceMethodClass ) ); - Assert.AreSame( instance.MethodAInfo, method ); - method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary( typeof( IMapsInterfaceMethodInterface ).GetMethod( "SomeMethodB" ), typeof( MapsInterfaceMethodClass ) ); - Assert.AreSame( instance.MethodBInfo, method ); - method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary( typeof( IMapsInterfaceMethodInterface ).GetProperty( "SomeProperty" ).GetGetMethod(), typeof( MapsInterfaceMethodClass ) ); - Assert.AreSame( instance.PropGetterInfo, method ); + method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary(typeof(MapsInterfaceMethodClass).GetMethod("SomeMethodA"), typeof(MapsInterfaceMethodClass)); + Assert.AreSame(instance.MethodAInfo, method); + method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary(typeof(IMapsInterfaceMethodInterface).GetMethod("SomeMethodA"), typeof(MapsInterfaceMethodClass)); + Assert.AreSame(instance.MethodAInfo, method); + method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary(typeof(IMapsInterfaceMethodInterface).GetMethod("SomeMethodB"), typeof(MapsInterfaceMethodClass)); + Assert.AreSame(instance.MethodBInfo, method); + method = ReflectionUtils.MapInterfaceMethodToImplementationIfNecessary(typeof(IMapsInterfaceMethodInterface).GetProperty("SomeProperty").GetGetMethod(), typeof(MapsInterfaceMethodClass)); + Assert.AreSame(instance.PropGetterInfo, method); } #region Helper class for http://jira.springframework.org/browse/SPRNET-992 tests @@ -210,14 +211,14 @@ namespace Spring.Util public readonly int b = -1; public readonly char c = '0'; - public Foo( string a, int b, char c ) + public Foo(string a, int b, char c) { this.a = a; this.b = b; this.c = c; } - public Foo( string a ) + public Foo(string a) { this.a = a; } @@ -229,242 +230,242 @@ namespace Spring.Util #endregion - [Test( Description = "http://jira.springframework.org/browse/SPRNET-992" )] + [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 ); + 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 ); + Assert.AreEqual("", foo.a); + Assert.AreEqual(-1, foo.b); + Assert.AreEqual('0', foo.c); } - [Test( Description = "http://jira.springframework.org/browse/SPRNET-992" )] + [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 ); + 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 ); + Assert.AreEqual("", foo.a); + Assert.AreEqual(-1, foo.b); + Assert.AreEqual('0', foo.c); } - [Test( Description = "http://jira.springframework.org/browse/SPRNET-992" )] + [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 ); + 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 ); + Assert.AreEqual("b", foo.a); + Assert.AreEqual(-1, foo.b); + Assert.AreEqual('0', foo.c); } [Test] public void GetParameterTypes() { - Type[] expectedParameterTypes = new Type[] { typeof( string ) }; - MethodInfo method = typeof( ReflectionUtilsObject ).GetMethod( "BadSpanglish" ); - Type[] parameterTypes = ReflectionUtils.GetParameterTypes( method ); - Assert.IsTrue( ArrayUtils.AreEqual( expectedParameterTypes, parameterTypes ) ); + Type[] expectedParameterTypes = new Type[] { typeof(string) }; + MethodInfo method = typeof(ReflectionUtilsObject).GetMethod("BadSpanglish"); + Type[] parameterTypes = ReflectionUtils.GetParameterTypes(method); + Assert.IsTrue(ArrayUtils.AreEqual(expectedParameterTypes, parameterTypes)); } [Test] - [ExpectedException( typeof( ArgumentNullException ) )] + [ExpectedException(typeof(ArgumentNullException))] public void GetParameterTypesWithNullMethodInfo() { - ReflectionUtils.GetParameterTypes( (MethodInfo)null ); + ReflectionUtils.GetParameterTypes((MethodInfo)null); } [Test] - [ExpectedException( typeof( ArgumentNullException ) )] + [ExpectedException(typeof(ArgumentNullException))] public void GetParameterTypesWithNullParametersArgs() { - ReflectionUtils.GetParameterTypes( (ParameterInfo[])null ); + ReflectionUtils.GetParameterTypes((ParameterInfo[])null); } [Test] - [ExpectedException( typeof( ArgumentNullException ) )] + [ExpectedException(typeof(ArgumentNullException))] public void GetMatchingMethodsWithNullTypeToFindOn() { - ReflectionUtils.GetMatchingMethods( null, new MethodInfo[] { }, true ); + ReflectionUtils.GetMatchingMethods(null, new MethodInfo[] { }, true); } [Test] - [ExpectedException( typeof( ArgumentNullException ) )] + [ExpectedException(typeof(ArgumentNullException))] public void GetMatchingMethodsWithNullMethodsToFind() { - ReflectionUtils.GetMatchingMethods( GetType(), null, true ); + ReflectionUtils.GetMatchingMethods(GetType(), null, true); } [Test] public void GetMatchingMethodsWithPerfectMatch() { - MethodInfo[] clonesMethods = typeof( ReflectionUtilsObjectClone ).GetMethods( BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance ); - MethodInfo[] foundMethods = ReflectionUtils.GetMatchingMethods( typeof( ReflectionUtilsObject ), clonesMethods, true ); - Assert.AreEqual( clonesMethods.Length, foundMethods.Length ); + MethodInfo[] clonesMethods = typeof(ReflectionUtilsObjectClone).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); + MethodInfo[] foundMethods = ReflectionUtils.GetMatchingMethods(typeof(ReflectionUtilsObject), clonesMethods, true); + Assert.AreEqual(clonesMethods.Length, foundMethods.Length); } [Test] - [ExpectedException( typeof( Exception ) )] + [ExpectedException(typeof(Exception))] public void GetMatchingMethodsWithBadMatchStrict() { // lets include a protected method that ain't declared on the ReflectionUtilsObject class... - MethodInfo[] clonesMethods = typeof( ReflectionUtilsObjectClone ).GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance ); - ReflectionUtils.GetMatchingMethods( typeof( ReflectionUtilsObject ), clonesMethods, true ); + MethodInfo[] clonesMethods = typeof(ReflectionUtilsObjectClone).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance); + ReflectionUtils.GetMatchingMethods(typeof(ReflectionUtilsObject), clonesMethods, true); } [Test] public void GetMatchingMethodsWithBadMatchNotStrict() { // lets include a protected method that ain't declared on the ReflectionUtilsObject class... - MethodInfo[] clonesMethods = typeof( ReflectionUtilsObjectClone ).GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance ); - MethodInfo[] foundMethods = ReflectionUtils.GetMatchingMethods( typeof( ReflectionUtilsObject ), clonesMethods, false ); + MethodInfo[] clonesMethods = typeof(ReflectionUtilsObjectClone).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance); + MethodInfo[] foundMethods = ReflectionUtils.GetMatchingMethods(typeof(ReflectionUtilsObject), clonesMethods, false); // obviously is not strict, 'cos we got here without throwing an exception... - Assert.AreEqual( clonesMethods.Length, foundMethods.Length ); + Assert.AreEqual(clonesMethods.Length, foundMethods.Length); } [Test] - [ExpectedException( typeof( Exception ) )] + [ExpectedException(typeof(Exception))] public void GetMatchingMethodsWithBadReturnTypeMatchStrict() { // lets include a method that return type is different... - MethodInfo[] clonesMethods = typeof( ReflectionUtilsObjectBadClone ).GetMethods( BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance ); - ReflectionUtils.GetMatchingMethods( typeof( ReflectionUtilsObject ), clonesMethods, true ); + MethodInfo[] clonesMethods = typeof(ReflectionUtilsObjectBadClone).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); + ReflectionUtils.GetMatchingMethods(typeof(ReflectionUtilsObject), clonesMethods, true); } [Test] public void GetMatchingMethodsWithBadReturnTypeMatchNotStrict() { // lets include a method that return type is different... - MethodInfo[] clonesMethods = typeof( ReflectionUtilsObjectBadClone ).GetMethods( BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance ); - MethodInfo[] foundMethods = ReflectionUtils.GetMatchingMethods( typeof( ReflectionUtilsObject ), clonesMethods, false ); + MethodInfo[] clonesMethods = typeof(ReflectionUtilsObjectBadClone).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); + MethodInfo[] foundMethods = ReflectionUtils.GetMatchingMethods(typeof(ReflectionUtilsObject), clonesMethods, false); // obviously is not strict, 'cos we got here without throwing an exception... - Assert.AreEqual( clonesMethods.Length, foundMethods.Length ); + Assert.AreEqual(clonesMethods.Length, foundMethods.Length); } [Test] public void ParameterTypesMatch() { - MethodInfo method = typeof( ReflectionUtilsObject ).GetMethod( "Spanglish" ); - Type[] types = new Type[] { typeof( string ), typeof( object[] ) }; - Assert.IsTrue( ReflectionUtils.ParameterTypesMatch( method, types ) ); + MethodInfo method = typeof(ReflectionUtilsObject).GetMethod("Spanglish"); + Type[] types = new Type[] { typeof(string), typeof(object[]) }; + Assert.IsTrue(ReflectionUtils.ParameterTypesMatch(method, types)); } [Test] public void ParameterTypesDontMatchWithNonMatchingArgs() { - Type[] types = new Type[] { typeof( string ), typeof( object[] ) }; + Type[] types = new Type[] { typeof(string), typeof(object[]) }; - MethodInfo method = typeof( ReflectionUtilsObject ).GetMethod( "BadSpanglish" ); - Assert.IsFalse( ReflectionUtils.ParameterTypesMatch( method, types ) ); - method = typeof( ReflectionUtilsObject ).GetMethod( "WickedSpanglish" ); - Assert.IsFalse( ReflectionUtils.ParameterTypesMatch( method, types ) ); + MethodInfo method = typeof(ReflectionUtilsObject).GetMethod("BadSpanglish"); + Assert.IsFalse(ReflectionUtils.ParameterTypesMatch(method, types)); + method = typeof(ReflectionUtilsObject).GetMethod("WickedSpanglish"); + Assert.IsFalse(ReflectionUtils.ParameterTypesMatch(method, types)); } [Test] public void GetDefaultValue() { - Assert.IsNull( ReflectionUtils.GetDefaultValue( GetType() ) ); - Assert.AreEqual( Cuts.Superficial, ReflectionUtils.GetDefaultValue( typeof( Cuts ) ) ); - Assert.AreEqual( false, ReflectionUtils.GetDefaultValue( typeof( bool ) ) ); - Assert.AreEqual( DateTime.MinValue, ReflectionUtils.GetDefaultValue( typeof( DateTime ) ) ); - Assert.AreEqual( Char.MinValue, ReflectionUtils.GetDefaultValue( typeof( char ) ) ); - Assert.AreEqual( 0, ReflectionUtils.GetDefaultValue( typeof( long ) ) ); - Assert.AreEqual( 0, ReflectionUtils.GetDefaultValue( typeof( int ) ) ); - Assert.AreEqual( 0, ReflectionUtils.GetDefaultValue( typeof( short ) ) ); + Assert.IsNull(ReflectionUtils.GetDefaultValue(GetType())); + Assert.AreEqual(Cuts.Superficial, ReflectionUtils.GetDefaultValue(typeof(Cuts))); + Assert.AreEqual(false, ReflectionUtils.GetDefaultValue(typeof(bool))); + Assert.AreEqual(DateTime.MinValue, ReflectionUtils.GetDefaultValue(typeof(DateTime))); + Assert.AreEqual(Char.MinValue, ReflectionUtils.GetDefaultValue(typeof(char))); + Assert.AreEqual(0, ReflectionUtils.GetDefaultValue(typeof(long))); + Assert.AreEqual(0, ReflectionUtils.GetDefaultValue(typeof(int))); + Assert.AreEqual(0, ReflectionUtils.GetDefaultValue(typeof(short))); } [Test] public void PropertyIsIndexer() { - Assert.IsTrue( ReflectionUtils.PropertyIsIndexer( "Item", typeof( TestObject ) ) ); - Assert.IsFalse( ReflectionUtils.PropertyIsIndexer( "Item", typeof( SideEffectObject ) ) ); - Assert.IsFalse( ReflectionUtils.PropertyIsIndexer( "Count", typeof( SideEffectObject ) ) ); - Assert.IsTrue( ReflectionUtils.PropertyIsIndexer( "MyItem", typeof( ObjectWithNonDefaultIndexerName ) ) ); + Assert.IsTrue(ReflectionUtils.PropertyIsIndexer("Item", typeof(TestObject))); + Assert.IsFalse(ReflectionUtils.PropertyIsIndexer("Item", typeof(SideEffectObject))); + Assert.IsFalse(ReflectionUtils.PropertyIsIndexer("Count", typeof(SideEffectObject))); + Assert.IsTrue(ReflectionUtils.PropertyIsIndexer("MyItem", typeof(ObjectWithNonDefaultIndexerName))); } [Test] public void MethodIsOnOneOfTheseInterfaces() { - MethodInfo method = typeof( ReflectionUtilsObject ).GetMethod( "Spanglish" ); - Assert.IsTrue( ReflectionUtils.MethodIsOnOneOfTheseInterfaces( method, new Type[] { typeof( IFoo ) } ) ); + MethodInfo method = typeof(ReflectionUtilsObject).GetMethod("Spanglish"); + Assert.IsTrue(ReflectionUtils.MethodIsOnOneOfTheseInterfaces(method, new Type[] { typeof(IFoo) })); } [Test] - [ExpectedException( typeof( ArgumentException ) )] + [ExpectedException(typeof(ArgumentException))] public void MethodIsOnOneOfTheseInterfacesWithNonInterfaceType() { - MethodInfo method = typeof( ReflectionUtilsObject ).GetMethod( "Spanglish" ); - Assert.IsFalse( ReflectionUtils.MethodIsOnOneOfTheseInterfaces( method, new Type[] { GetType() } ) ); + MethodInfo method = typeof(ReflectionUtilsObject).GetMethod("Spanglish"); + Assert.IsFalse(ReflectionUtils.MethodIsOnOneOfTheseInterfaces(method, new Type[] { GetType() })); } [Test] - [ExpectedException( typeof( ArgumentNullException ) )] + [ExpectedException(typeof(ArgumentNullException))] public void MethodIsOnOneOfTheseInterfacesWithNullMethod() { MethodInfo method = null; - Assert.IsFalse( ReflectionUtils.MethodIsOnOneOfTheseInterfaces( method, new Type[] { GetType() } ) ); + Assert.IsFalse(ReflectionUtils.MethodIsOnOneOfTheseInterfaces(method, new Type[] { GetType() })); } [Test] public void MethodIsOnOneOfTheseInterfacesWithNullTypes() { - MethodInfo method = typeof( ReflectionUtilsObject ).GetMethod( "Spanglish" ); - Assert.IsFalse( ReflectionUtils.MethodIsOnOneOfTheseInterfaces( method, null ) ); + MethodInfo method = typeof(ReflectionUtilsObject).GetMethod("Spanglish"); + Assert.IsFalse(ReflectionUtils.MethodIsOnOneOfTheseInterfaces(method, null)); } [Test] public void MethodIsOnOneOfTheseInterfacesMultiple() { - MethodInfo method = typeof( RequiredTestObject ).GetMethod( "set_ObjectFactory" ); - Assert.IsNotNull( method, "Could not get setter property for ObjectFactory" ); - Assert.IsTrue( ReflectionUtils.MethodIsOnOneOfTheseInterfaces( method, new Type[] { typeof( IObjectNameAware ), typeof( IObjectFactoryAware ) } ) ); + MethodInfo method = typeof(RequiredTestObject).GetMethod("set_ObjectFactory"); + Assert.IsNotNull(method, "Could not get setter property for ObjectFactory"); + Assert.IsTrue(ReflectionUtils.MethodIsOnOneOfTheseInterfaces(method, new Type[] { typeof(IObjectNameAware), typeof(IObjectFactoryAware) })); } [Test] - [ExpectedException( typeof( ArgumentNullException ) )] + [ExpectedException(typeof(ArgumentNullException))] public void ParameterTypesMatchWithNullArgs() { - ReflectionUtils.ParameterTypesMatch( null, null ); + ReflectionUtils.ParameterTypesMatch(null, null); } [Test] public void GetSignature() { - MethodInfo method = typeof( ReflectionUtilsObject ).GetMethod( "Spanglish" ); + MethodInfo method = typeof(ReflectionUtilsObject).GetMethod("Spanglish"); ArrayList list = new ArrayList(); foreach (ParameterInfo p in method.GetParameters()) { - list.Add( p.ParameterType ); + list.Add(p.ParameterType); } string expected = "Spring.Util.ReflectionUtilsObject::Spanglish(System.String,System.Object[])"; - Type[] pTypes = (Type[])list.ToArray( typeof( Type ) ); - string actual = ReflectionUtils.GetSignature( method.DeclaringType, method.Name, pTypes ); - Assert.AreEqual( expected, actual ); + Type[] pTypes = (Type[])list.ToArray(typeof(Type)); + string actual = ReflectionUtils.GetSignature(method.DeclaringType, method.Name, pTypes); + Assert.AreEqual(expected, actual); } [Test] public void ToInterfaceArrayFromType() { - Type[] expected = new Type[] { typeof( IFoo ), typeof( IBar ) }; - Type[] actual = ReflectionUtils.ToInterfaceArray( typeof( IBar ) ); - Assert.AreEqual( expected.Length, actual.Length ); - Assert.AreEqual( expected[0], actual[0] ); - Assert.AreEqual( expected[1], actual[1] ); + Type[] expected = new Type[] { typeof(IFoo), typeof(IBar) }; + Type[] actual = ReflectionUtils.ToInterfaceArray(typeof(IBar)); + Assert.AreEqual(expected.Length, actual.Length); + Assert.AreEqual(expected[0], actual[0]); + Assert.AreEqual(expected[1], actual[1]); } [Test] - [ExpectedException( typeof( ArgumentException ) )] + [ExpectedException(typeof(ArgumentException))] public void ToInterfaceArrayFromTypeWithNonInterface() { - ReflectionUtils.ToInterfaceArray( typeof( ExplicitFoo ) ); + ReflectionUtils.ToInterfaceArray(typeof(ExplicitFoo)); } @@ -472,45 +473,45 @@ namespace Spring.Util public void GetMethod() { MethodInfo actual = ReflectionUtils.GetMethod( - typeof( ReflectionUtilsObject ), + typeof(ReflectionUtilsObject), "Spanglish", - new Type[] { typeof( string ), typeof( object[] ) } ); - Assert.IsNotNull( actual ); + new Type[] { typeof(string), typeof(object[]) }); + Assert.IsNotNull(actual); } [Test] public void GetMethodWithExplicitInterfaceMethod() { MethodInfo actual = ReflectionUtils.GetMethod( - typeof( ExplicitFoo ), + typeof(ExplicitFoo), "Spring.Util.IFoo.Spanglish", - new Type[] { typeof( string ), typeof( object[] ) } ); - Assert.IsNotNull( actual ); + new Type[] { typeof(string), typeof(object[]) }); + Assert.IsNotNull(actual); } [Test] public void GetMethodIsCaseInsensitive() { MethodInfo actual = ReflectionUtils.GetMethod( - typeof( ReflectionUtilsObject ), + typeof(ReflectionUtilsObject), "spAngLISh", - new Type[] { typeof( string ), typeof( object[] ) } ); - Assert.IsNotNull( actual, "ReflectionUtils.GetMethod would appear to be case sensitive." ); + new Type[] { typeof(string), typeof(object[]) }); + Assert.IsNotNull(actual, "ReflectionUtils.GetMethod would appear to be case sensitive."); } [Test] - [ExpectedException( typeof( ArgumentException ), - ExpectedMessage = "[Spring.Util.ReflectionUtilsTests] does not derive from the [System.Attribute] class." )] + [ExpectedException(typeof(ArgumentException), + ExpectedMessage = "[Spring.Util.ReflectionUtilsTests] does not derive from the [System.Attribute] class.")] public void CreateCustomAttributeForNonAttributeType() { - ReflectionUtils.CreateCustomAttribute( GetType() ); + ReflectionUtils.CreateCustomAttribute(GetType()); } [Test] - [ExpectedException( typeof( ArgumentNullException ) )] + [ExpectedException(typeof(ArgumentNullException))] public void CreateCustomAttributeWithNullType() { - ReflectionUtils.CreateCustomAttribute( (Type)null ); + ReflectionUtils.CreateCustomAttribute((Type)null); } [Test] @@ -518,23 +519,23 @@ namespace Spring.Util { CustomAttributeBuilder builder = null; - builder = ReflectionUtils.CreateCustomAttribute( typeof( MyCustomAttribute ) ); - Assert.IsNotNull( builder ); + builder = ReflectionUtils.CreateCustomAttribute(typeof(MyCustomAttribute)); + Assert.IsNotNull(builder); - builder = ReflectionUtils.CreateCustomAttribute( typeof( MyCustomAttribute ), "Rick" ); - Assert.IsNotNull( builder ); + builder = ReflectionUtils.CreateCustomAttribute(typeof(MyCustomAttribute), "Rick"); + Assert.IsNotNull(builder); - builder = ReflectionUtils.CreateCustomAttribute( typeof( MyCustomAttribute ), "Rick" ); - Assert.IsNotNull( builder ); + builder = ReflectionUtils.CreateCustomAttribute(typeof(MyCustomAttribute), "Rick"); + Assert.IsNotNull(builder); - builder = ReflectionUtils.CreateCustomAttribute( new MyCustomAttribute( "Rick" ) ); - Assert.IsNotNull( builder ); + builder = ReflectionUtils.CreateCustomAttribute(new MyCustomAttribute("Rick")); + Assert.IsNotNull(builder); - builder = ReflectionUtils.CreateCustomAttribute( typeof( MyCustomAttribute ), new MyCustomAttribute( "Rick" ) ); - Assert.IsNotNull( builder ); + builder = ReflectionUtils.CreateCustomAttribute(typeof(MyCustomAttribute), new MyCustomAttribute("Rick")); + Assert.IsNotNull(builder); - builder = ReflectionUtils.CreateCustomAttribute( typeof( MyCustomAttribute ), new object[] { "Rick" }, new MyCustomAttribute( "Evans" ) ); - Assert.IsNotNull( builder ); + builder = ReflectionUtils.CreateCustomAttribute(typeof(MyCustomAttribute), new object[] { "Rick" }, new MyCustomAttribute("Evans")); + Assert.IsNotNull(builder); // TODO : actually emit the attribute and check it... } @@ -542,45 +543,164 @@ namespace Spring.Util [Test] public void CreatCustomAttriubtesFromCustomAttributeData() { - Type control = typeof( Control ); - MethodInfo mi = control.GetMethod( "get_Font" ); - System.Collections.Generic.IList attributes = CustomAttributeData.GetCustomAttributes( mi.ReturnParameter ); + Type control = typeof(Control); + MethodInfo mi = control.GetMethod("get_Font"); + System.Collections.Generic.IList attributes = CustomAttributeData.GetCustomAttributes(mi.ReturnParameter); CustomAttributeBuilder builder = null; foreach (CustomAttributeData customAttributeData in attributes) { - builder = ReflectionUtils.CreateCustomAttribute( customAttributeData ); - Assert.IsNotNull( builder ); + builder = ReflectionUtils.CreateCustomAttribute(customAttributeData); + Assert.IsNotNull(builder); } } + + [Test] + public void CreatCustomAttriubtesFromCustomAttributeDataWithSingleEnum() + { + MethodInfo mi = typeof(TestClassHavingAttributeWithEnum).GetMethod("SomeMethod"); + System.Collections.Generic.IList attributes = CustomAttributeData.GetCustomAttributes(mi); + CustomAttributeBuilder builder = null; + foreach (CustomAttributeData customAttributeData in attributes) + { + builder = ReflectionUtils.CreateCustomAttribute(customAttributeData); + Assert.IsNotNull(builder); + } + } + + [Test] + public void CreatCustomAttriubtesFromCustomAttributeDataWithArrayOfEnumsSetOnProperty() + { + MethodInfo mi = typeof(TestClassHavingAttributeWithEnumArraySetOnProperty).GetMethod("SomeMethod"); + System.Collections.Generic.IList attributes = CustomAttributeData.GetCustomAttributes(mi); + CustomAttributeBuilder builder = null; + foreach (CustomAttributeData customAttributeData in attributes) + { + builder = ReflectionUtils.CreateCustomAttribute(customAttributeData); + Assert.IsNotNull(builder); + } + } + + [Test] + public void CreatCustomAttriubtesFromCustomAttributeDataWithArrayOfEnumsSetInConstructor() + { + MethodInfo mi = typeof(TestClassHavingAttributeWithEnumArraySetInConstructor).GetMethod("SomeMethod"); + System.Collections.Generic.IList attributes = CustomAttributeData.GetCustomAttributes(mi); + CustomAttributeBuilder builder = null; + foreach (CustomAttributeData customAttributeData in attributes) + { + builder = ReflectionUtils.CreateCustomAttribute(customAttributeData); + Assert.IsNotNull(builder); + } + } + + internal interface IHaveSomeMethod + { + void SomeMethod(); + } + + internal class TestClassHavingAttributeWithEnum : IHaveSomeMethod + { + [AttributeWithEnum(SomeProperty = TheTestEnumThing.One)] + public void SomeMethod() + { + + } + } + + + internal class TestClassHavingAttributeWithEnumArraySetInConstructor : IHaveSomeMethod + { + [AttributeWithEnumArraySetInConstructor(new[] { TheTestEnumThing.One, TheTestEnumThing.Three })] + public void SomeMethod() + { + + } + } + + internal class TestClassHavingAttributeWithEnumArraySetOnProperty : IHaveSomeMethod + { + [AttributeWithEnumArray(SomeProperty = new[] { TheTestEnumThing.One, TheTestEnumThing.Three })] + public void SomeMethod() + { + + } + } + + internal enum TheTestEnumThing + { + One, Two, Three + } + + internal class AttributeWithEnumArrayAttribute : Attribute + { + private TheTestEnumThing[] _someProperty; + public TheTestEnumThing[] SomeProperty + { + get { return _someProperty; } + set + { + _someProperty = value; + } + } + + } + + internal class AttributeWithEnumArraySetInConstructorAttribute : Attribute + { + /// + /// Initializes a new instance of the AttributeWithEnumArraySetInConstructorAttribute class. + /// + public AttributeWithEnumArraySetInConstructorAttribute(TheTestEnumThing[] things) + { + + } + + } + + class AttributeWithEnumAttribute : Attribute + { + private TheTestEnumThing _someProperty; + public TheTestEnumThing SomeProperty + { + get { return _someProperty; } + set + { + _someProperty = value; + } + } + + } + + #endif [Test] public void CreateCustomAttributeUsingDefaultValuesForTheConstructor() { CustomAttributeBuilder builder = null; - builder = ReflectionUtils.CreateCustomAttribute( typeof( AnotherCustomAttribute ) ); - Assert.IsNotNull( builder ); + builder = ReflectionUtils.CreateCustomAttribute(typeof(AnotherCustomAttribute)); + Assert.IsNotNull(builder); AnotherCustomAttribute att - = (AnotherCustomAttribute)CheckForPresenceOfCustomAttribute( builder, typeof( AnotherCustomAttribute ) ); - Assert.IsNull( att.Name ); - Assert.AreEqual( 0, att.Age ); - Assert.IsFalse( att.HasSwallowedExplosives ); + = (AnotherCustomAttribute)CheckForPresenceOfCustomAttribute(builder, typeof(AnotherCustomAttribute)); + Assert.IsNull(att.Name); + Assert.AreEqual(0, att.Age); + Assert.IsFalse(att.HasSwallowedExplosives); } [Test] public void CreateCustomAttributeFromSourceAttribute() { CustomAttributeBuilder builder = null; - AnotherCustomAttribute source = new AnotherCustomAttribute( "Rick", 30, true ); - builder = ReflectionUtils.CreateCustomAttribute( source ); - Assert.IsNotNull( builder ); + AnotherCustomAttribute source = new AnotherCustomAttribute("Rick", 30, true); + builder = ReflectionUtils.CreateCustomAttribute(source); + Assert.IsNotNull(builder); AnotherCustomAttribute att - = (AnotherCustomAttribute)CheckForPresenceOfCustomAttribute( builder, typeof( AnotherCustomAttribute ) ); - Assert.AreEqual( source.Name, att.Name ); - Assert.AreEqual( source.Age, att.Age ); - Assert.AreEqual( source.HasSwallowedExplosives, att.HasSwallowedExplosives ); + = (AnotherCustomAttribute)CheckForPresenceOfCustomAttribute(builder, typeof(AnotherCustomAttribute)); + Assert.AreEqual(source.Name, att.Name); + Assert.AreEqual(source.Age, att.Age); + Assert.AreEqual(source.HasSwallowedExplosives, att.HasSwallowedExplosives); } [Test] @@ -589,17 +709,17 @@ namespace Spring.Util CustomAttributeBuilder builder = null; const string expectedName = "Rick"; const int expectedAge = 30; - builder = ReflectionUtils.CreateCustomAttribute( typeof( AnotherCustomAttribute ), new object[] + builder = ReflectionUtils.CreateCustomAttribute(typeof(AnotherCustomAttribute), new object[] { expectedName, expectedAge, true - } ); - Assert.IsNotNull( builder ); + }); + Assert.IsNotNull(builder); AnotherCustomAttribute att - = (AnotherCustomAttribute)CheckForPresenceOfCustomAttribute( builder, typeof( AnotherCustomAttribute ) ); - Assert.AreEqual( expectedName, att.Name ); - Assert.AreEqual( expectedAge, att.Age ); - Assert.IsTrue( att.HasSwallowedExplosives ); + = (AnotherCustomAttribute)CheckForPresenceOfCustomAttribute(builder, typeof(AnotherCustomAttribute)); + Assert.AreEqual(expectedName, att.Name); + Assert.AreEqual(expectedAge, att.Age); + Assert.IsTrue(att.HasSwallowedExplosives); } [Test] @@ -608,121 +728,121 @@ namespace Spring.Util CustomAttributeBuilder builder = null; const string expectedName = "Rick"; const int expectedAge = 30; - AnotherCustomAttribute source = new AnotherCustomAttribute( expectedName, expectedAge, false ); - builder = ReflectionUtils.CreateCustomAttribute( typeof( AnotherCustomAttribute ), new object[] + AnotherCustomAttribute source = new AnotherCustomAttribute(expectedName, expectedAge, false); + builder = ReflectionUtils.CreateCustomAttribute(typeof(AnotherCustomAttribute), new object[] { "Hoop", 2, true - }, source ); - Assert.IsNotNull( builder ); + }, source); + Assert.IsNotNull(builder); AnotherCustomAttribute att - = (AnotherCustomAttribute)CheckForPresenceOfCustomAttribute( builder, typeof( AnotherCustomAttribute ) ); - Assert.AreEqual( expectedName, att.Name ); - Assert.AreEqual( expectedAge, att.Age ); - Assert.IsFalse( att.HasSwallowedExplosives ); + = (AnotherCustomAttribute)CheckForPresenceOfCustomAttribute(builder, typeof(AnotherCustomAttribute)); + Assert.AreEqual(expectedName, att.Name); + Assert.AreEqual(expectedAge, att.Age); + Assert.IsFalse(att.HasSwallowedExplosives); } [Test] public void HasAtLeastOneMethodWithName() { - Type testType = typeof( ExtendedReflectionUtilsObject ); + Type testType = typeof(ExtendedReflectionUtilsObject); // declared method... - Assert.IsTrue( ReflectionUtils.HasAtLeastOneMethodWithName( testType, "Declared" ) ); + Assert.IsTrue(ReflectionUtils.HasAtLeastOneMethodWithName(testType, "Declared")); // case insensitive method... - Assert.IsTrue( ReflectionUtils.HasAtLeastOneMethodWithName( testType, "deCLAReD" ) ); + Assert.IsTrue(ReflectionUtils.HasAtLeastOneMethodWithName(testType, "deCLAReD")); // superclass method... - Assert.IsTrue( ReflectionUtils.HasAtLeastOneMethodWithName( testType, "Spanglish" ) ); + Assert.IsTrue(ReflectionUtils.HasAtLeastOneMethodWithName(testType, "Spanglish")); // static method... - Assert.IsTrue( ReflectionUtils.HasAtLeastOneMethodWithName( testType, "Static" ) ); + Assert.IsTrue(ReflectionUtils.HasAtLeastOneMethodWithName(testType, "Static")); // protected method... - Assert.IsTrue( ReflectionUtils.HasAtLeastOneMethodWithName( testType, "Protected" ) ); + Assert.IsTrue(ReflectionUtils.HasAtLeastOneMethodWithName(testType, "Protected")); // non existent method... - Assert.IsFalse( ReflectionUtils.HasAtLeastOneMethodWithName( testType, "Sponglish" ) ); + Assert.IsFalse(ReflectionUtils.HasAtLeastOneMethodWithName(testType, "Sponglish")); // null type... - Assert.IsFalse( ReflectionUtils.HasAtLeastOneMethodWithName( null, "Spanglish" ) ); + Assert.IsFalse(ReflectionUtils.HasAtLeastOneMethodWithName(null, "Spanglish")); // null method name... - Assert.IsFalse( ReflectionUtils.HasAtLeastOneMethodWithName( testType, null ) ); + Assert.IsFalse(ReflectionUtils.HasAtLeastOneMethodWithName(testType, null)); // empty method name... - Assert.IsFalse( ReflectionUtils.HasAtLeastOneMethodWithName( testType, "" ) ); + Assert.IsFalse(ReflectionUtils.HasAtLeastOneMethodWithName(testType, "")); // all nulls... - Assert.IsFalse( ReflectionUtils.HasAtLeastOneMethodWithName( null, null ) ); + Assert.IsFalse(ReflectionUtils.HasAtLeastOneMethodWithName(null, null)); } [Test] - [ExpectedException( typeof( NullReferenceException ) )] + [ExpectedException(typeof(NullReferenceException))] public void GetTypeOfOrTypeWithNull() { - ReflectionUtils.TypeOfOrType( null ); + ReflectionUtils.TypeOfOrType(null); } [Test] public void GetTypeOfOrType() { - Assert.AreEqual( typeof( string ), ReflectionUtils.TypeOfOrType( typeof( string ) ) ); + Assert.AreEqual(typeof(string), ReflectionUtils.TypeOfOrType(typeof(string))); } [Test] public void GetTypeOfOrTypeWithNonNullType() { - Assert.AreEqual( typeof( string ), ReflectionUtils.TypeOfOrType( string.Empty ) ); + Assert.AreEqual(typeof(string), ReflectionUtils.TypeOfOrType(string.Empty)); } [Test] public void GetTypes() { Type[] actual = ReflectionUtils.GetTypes( - new object[] { 1, "I've never been to Taco Bell (sighs).", new ReflectionUtilsObject() } ); + new object[] { 1, "I've never been to Taco Bell (sighs).", new ReflectionUtilsObject() }); Type[] expected = new Type[] { typeof (int), typeof (string), typeof (ReflectionUtilsObject), }; - Assert.IsTrue( ArrayUtils.AreEqual( expected, actual ), "The ReflectionUtils.GetTypes method did not return a correct Type [] array. Yep, that's as helpful as it gets." ); + Assert.IsTrue(ArrayUtils.AreEqual(expected, actual), "The ReflectionUtils.GetTypes method did not return a correct Type [] array. Yep, that's as helpful as it gets."); } [Test] public void GetTypesWithEmptyArrayArgument() { - Type[] actual = ReflectionUtils.GetTypes( new object[] { } ); - Assert.IsNotNull( actual, "The ReflectionUtils.GetTypes method returned null when given an empty array. Must return an empty Type [] array." ); - Assert.IsTrue( actual.Length == 0, "The ReflectionUtils.GetTypes method returned a Type [] that had some elements in it when given an empty array. Must return an empty Type [] array." ); + Type[] actual = ReflectionUtils.GetTypes(new object[] { }); + Assert.IsNotNull(actual, "The ReflectionUtils.GetTypes method returned null when given an empty array. Must return an empty Type [] array."); + Assert.IsTrue(actual.Length == 0, "The ReflectionUtils.GetTypes method returned a Type [] that had some elements in it when given an empty array. Must return an empty Type [] array."); } [Test] public void GetTypesWithNullArrayArgument() { - Type[] actual = ReflectionUtils.GetTypes( null ); - Assert.IsNotNull( actual, "The ReflectionUtils.GetTypes method returned null when given null. Must return an empty Type [] array." ); - Assert.IsTrue( actual.Length == 0, "The ReflectionUtils.GetTypes method returned a Type [] that had some elements in it when given null. Must return an empty Type [] array." ); + Type[] actual = ReflectionUtils.GetTypes(null); + Assert.IsNotNull(actual, "The ReflectionUtils.GetTypes method returned null when given null. Must return an empty Type [] array."); + Assert.IsTrue(actual.Length == 0, "The ReflectionUtils.GetTypes method returned a Type [] that had some elements in it when given null. Must return an empty Type [] array."); } [Test] - [ExpectedException( typeof( ArgumentException ) )] + [ExpectedException(typeof(ArgumentException))] public void GetDefaultValueWithZeroValueEnumType() { - ReflectionUtils.GetDefaultValue( typeof( EnumWithNoValues ) ); + ReflectionUtils.GetDefaultValue(typeof(EnumWithNoValues)); } [Test] public void GetParameterTypesWithMethodThatHasRefParameters() { - MethodInfo method = GetType().GetMethod( "Add" ); - Type[] types = ReflectionUtils.GetParameterTypes( method ); - Assert.IsNotNull( types ); - Assert.AreEqual( 2, types.Length ); + MethodInfo method = GetType().GetMethod("Add"); + Type[] types = ReflectionUtils.GetParameterTypes(method); + Assert.IsNotNull(types); + Assert.AreEqual(2, types.Length); // first method parameter is byRef, so type name must end in '&' - Assert.AreEqual( "System.Int32&", types[0].FullName ); - Assert.AreEqual( "System.Int32", types[1].FullName ); + Assert.AreEqual("System.Int32&", types[0].FullName); + Assert.AreEqual("System.Int32", types[1].FullName); } [Test] public void GetMethodByArgumentValuesResolvesToExactMatchIfAvailable() { GetMethodByArgumentValuesTarget.DummyArgumentType[] typedArg = new GetMethodByArgumentValuesTarget.DummyArgumentType[] { }; - GetMethodByArgumentValuesTarget foo = new GetMethodByArgumentValuesTarget( 1, typedArg ); + GetMethodByArgumentValuesTarget foo = new GetMethodByArgumentValuesTarget(1, typedArg); - Type type = typeof( GetMethodByArgumentValuesTarget ); + Type type = typeof(GetMethodByArgumentValuesTarget); MethodInfo[] candidateMethods = new MethodInfo[] { type.GetMethod("MethodWithSimilarArguments", new Type[] {typeof(int), typeof(ICollection)}) @@ -731,22 +851,22 @@ namespace Spring.Util }; // 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 ) ); + 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 ); + MethodInfo resolvedMethod = ReflectionUtils.GetMethodByArgumentValues(candidateMethods, new object[] { 1, typedArg }); + Assert.AreSame(candidateMethods[1], resolvedMethod); } [Test] public void GetConstructorByArgumentValuesResolvesToExactMatchIfAvailable() { GetMethodByArgumentValuesTarget.DummyArgumentType[] typedArg = new GetMethodByArgumentValuesTarget.DummyArgumentType[] { }; - Type type = typeof( GetMethodByArgumentValuesTarget ); + Type type = typeof(GetMethodByArgumentValuesTarget); ConstructorInfo[] candidateConstructors = new ConstructorInfo[] { type.GetConstructor(new Type[] {typeof(int), typeof(ICollection)}) @@ -755,15 +875,15 @@ namespace Spring.Util }; // 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 ); + 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 ); + ConstructorInfo resolvedConstructor = ReflectionUtils.GetConstructorByArgumentValues(candidateConstructors, new object[] { 1, typedArg }); + Assert.AreSame(candidateConstructors[1], resolvedConstructor); } #region GetInterfaces @@ -772,11 +892,11 @@ namespace Spring.Util public void GetInterfaces() { Assert.AreEqual( - typeof( TestObject ).GetInterfaces().Length, - ReflectionUtils.GetInterfaces( typeof( TestObject ) ).Length ); + typeof(TestObject).GetInterfaces().Length, + ReflectionUtils.GetInterfaces(typeof(TestObject)).Length); - Assert.AreEqual( 1, ReflectionUtils.GetInterfaces( typeof( IInterface1 ) ).Length ); - Assert.AreEqual( 4, ReflectionUtils.GetInterfaces( typeof( IInterface2 ) ).Length ); + Assert.AreEqual(1, ReflectionUtils.GetInterfaces(typeof(IInterface1)).Length); + Assert.AreEqual(4, ReflectionUtils.GetInterfaces(typeof(IInterface2)).Length); } public interface IInterface1 @@ -806,85 +926,85 @@ namespace Spring.Util [Test] public void IsTypeVisibleWithInternalType() { - Type type = typeof( InternalType ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(InternalType); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithPublicNestedTypeOnInternalType() { - Type type = typeof( InternalType.PublicNestedType ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(InternalType.PublicNestedType); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithInternalNestedTypeOnInternalType() { - Type type = typeof( InternalType.InternalNestedType ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(InternalType.InternalNestedType); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithProtectedInternalNestedTypeOnInternalType() { - Type type = typeof( InternalType.ProtectedInternalNestedType ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(InternalType.ProtectedInternalNestedType); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithProtectedNestedTypeOnInternalType() { - Type type = typeof( InternalType ).GetNestedType( "ProtectedNestedType", BindingFlags.NonPublic ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(InternalType).GetNestedType("ProtectedNestedType", BindingFlags.NonPublic); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithPrivateNestedTypeOnInternalType() { - Type type = typeof( InternalType ).GetNestedType( "PrivateNestedType", BindingFlags.NonPublic ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(InternalType).GetNestedType("PrivateNestedType", BindingFlags.NonPublic); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithPublicType() { - Type type = typeof( PublicType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(PublicType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithPublicNestedTypeOnPublicType() { - Type type = typeof( PublicType.PublicNestedType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(PublicType.PublicNestedType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithInternalNestedTypeOnPublicType() { - Type type = typeof( PublicType.InternalNestedType ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(PublicType.InternalNestedType); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithProtectedInternalNestedTypeOnPublicType() { - Type type = typeof( PublicType.ProtectedInternalNestedType ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(PublicType.ProtectedInternalNestedType); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithProtectedNestedTypeOnPublicType() { - Type type = typeof( PublicType ).GetNestedType( "ProtectedNestedType", BindingFlags.NonPublic ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(PublicType).GetNestedType("ProtectedNestedType", BindingFlags.NonPublic); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } [Test] public void IsTypeVisibleWithPrivateNestedTypeOnPublicType() { - Type type = typeof( PublicType ).GetNestedType( "PrivateNestedType", BindingFlags.NonPublic ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type ) ); + Type type = typeof(PublicType).GetNestedType("PrivateNestedType", BindingFlags.NonPublic); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type)); } #if NET_2_0 @@ -893,85 +1013,85 @@ namespace Spring.Util [Test] public void IsTypeVisibleFromFriendlyAssemblyWithInternalType() { - Type type = typeof( InternalType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(InternalType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithPublicNestedTypeOnInternalType() { - Type type = typeof( InternalType.PublicNestedType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(InternalType.PublicNestedType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithInternalNestedTypeOnInternalType() { - Type type = typeof( InternalType.InternalNestedType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(InternalType.InternalNestedType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithProtectedInternalNestedTypeOnInternalType() { - Type type = typeof( InternalType.ProtectedInternalNestedType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(InternalType.ProtectedInternalNestedType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithProtectedNestedTypeOnInternalType() { - Type type = typeof( InternalType ).GetNestedType( "ProtectedNestedType", BindingFlags.NonPublic ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(InternalType).GetNestedType("ProtectedNestedType", BindingFlags.NonPublic); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithPrivateNestedTypeOnInternalType() { - Type type = typeof( InternalType ).GetNestedType( "PrivateNestedType", BindingFlags.NonPublic ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(InternalType).GetNestedType("PrivateNestedType", BindingFlags.NonPublic); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithPublicType() { - Type type = typeof( PublicType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(PublicType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithPublicNestedTypeOnPublicType() { - Type type = typeof( PublicType.PublicNestedType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(PublicType.PublicNestedType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithInternalNestedTypeOnPublicType() { - Type type = typeof( PublicType.InternalNestedType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(PublicType.InternalNestedType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithProtectedInternalNestedTypeOnPublicType() { - Type type = typeof( PublicType.ProtectedInternalNestedType ); - Assert.IsTrue( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(PublicType.ProtectedInternalNestedType); + Assert.IsTrue(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithProtectedNestedTypeOnPublicType() { - Type type = typeof( PublicType ).GetNestedType( "ProtectedNestedType", BindingFlags.NonPublic ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(PublicType).GetNestedType("ProtectedNestedType", BindingFlags.NonPublic); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } [Test] public void IsTypeVisibleFromFriendlyAssemblyWithPrivateNestedTypeOnPublicType() { - Type type = typeof( PublicType ).GetNestedType( "PrivateNestedType", BindingFlags.NonPublic ); - Assert.IsFalse( ReflectionUtils.IsTypeVisible( type, FRIENDLY_ASSEMBLY_NAME ) ); + Type type = typeof(PublicType).GetNestedType("PrivateNestedType", BindingFlags.NonPublic); + Assert.IsFalse(ReflectionUtils.IsTypeVisible(type, FRIENDLY_ASSEMBLY_NAME)); } #endif @@ -1009,79 +1129,79 @@ namespace Spring.Util public void GetExplicitBaseExceptionWithNoInnerException() { Exception appEx = new ApplicationException(); - Exception ex = ReflectionUtils.GetExplicitBaseException( appEx ); + Exception ex = ReflectionUtils.GetExplicitBaseException(appEx); - Assert.AreEqual( ex, appEx ); + Assert.AreEqual(ex, appEx); } [Test] public void GetExplicitBaseExceptionWithInnerException() { Exception dbzEx = new DivideByZeroException(); - Exception appEx = new ApplicationException( "Test message", dbzEx ); - Exception ex = ReflectionUtils.GetExplicitBaseException( appEx ); + Exception appEx = new ApplicationException("Test message", dbzEx); + Exception ex = ReflectionUtils.GetExplicitBaseException(appEx); - Assert.AreEqual( ex, dbzEx ); + Assert.AreEqual(ex, dbzEx); } [Test] public void GetExplicitBaseExceptionWithInnerExceptions() { Exception dbzEx = new DivideByZeroException(); - Exception sEx = new SystemException( "Test message", dbzEx ); - Exception appEx = new ApplicationException( "Test message", sEx ); - Exception ex = ReflectionUtils.GetExplicitBaseException( appEx ); + Exception sEx = new SystemException("Test message", dbzEx); + Exception appEx = new ApplicationException("Test message", sEx); + Exception ex = ReflectionUtils.GetExplicitBaseException(appEx); - Assert.AreEqual( ex, dbzEx ); + Assert.AreEqual(ex, dbzEx); } [Test] public void GetExplicitBaseExceptionWithNullReferenceExceptionAsRootException() { Exception nrEx = new NullReferenceException(); - Exception appEx = new ApplicationException( "Test message", nrEx ); - Exception ex = ReflectionUtils.GetExplicitBaseException( appEx ); + Exception appEx = new ApplicationException("Test message", nrEx); + Exception ex = ReflectionUtils.GetExplicitBaseException(appEx); - Assert.AreEqual( ex, appEx ); + Assert.AreEqual(ex, appEx); } #endregion #region Helper Methods - public int Add( ref int one, int two ) + public int Add(ref int one, int two) { return one + two; } private Attribute CheckForPresenceOfCustomAttribute( - CustomAttributeBuilder attBuilder, Type attType ) + CustomAttributeBuilder attBuilder, Type attType) { - Attribute[] atts = ApplyAndGetCustomAttributes( attBuilder ); - Assert.IsNotNull( atts ); - Assert.IsTrue( atts.Length == 1 ); + Attribute[] atts = ApplyAndGetCustomAttributes(attBuilder); + Assert.IsNotNull(atts); + Assert.IsTrue(atts.Length == 1); Attribute att = atts[0]; - Assert.IsNotNull( att ); - Assert.AreEqual( attType, att.GetType(), "Wrong Attribute applied to the class." ); + Assert.IsNotNull(att); + Assert.AreEqual(attType, att.GetType(), "Wrong Attribute applied to the class."); return att; } - private static Attribute[] ApplyAndGetCustomAttributes( CustomAttributeBuilder attBuilder ) + private static Attribute[] ApplyAndGetCustomAttributes(CustomAttributeBuilder attBuilder) { - Type type = BuildTypeWithThisCustomAttribute( attBuilder ); - object[] attributes = type.GetCustomAttributes( true ); - return (Attribute[])ArrayList.Adapter( attributes ).ToArray( typeof( Attribute ) ); + Type type = BuildTypeWithThisCustomAttribute(attBuilder); + object[] attributes = type.GetCustomAttributes(true); + return (Attribute[])ArrayList.Adapter(attributes).ToArray(typeof(Attribute)); } - private static Type BuildTypeWithThisCustomAttribute( CustomAttributeBuilder attBuilder ) + private static Type BuildTypeWithThisCustomAttribute(CustomAttributeBuilder attBuilder) { AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = "AnAssembly"; AssemblyBuilder asmBuilder = Thread.GetDomain().DefineDynamicAssembly( - assemblyName, AssemblyBuilderAccess.Run ); - ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule( "AModule" ); - TypeBuilder classBuilder = modBuilder.DefineType( "AClass", TypeAttributes.Public ); - classBuilder.SetCustomAttribute( attBuilder ); + assemblyName, AssemblyBuilderAccess.Run); + ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("AModule"); + TypeBuilder classBuilder = modBuilder.DefineType("AClass", TypeAttributes.Public); + classBuilder.SetCustomAttribute(attBuilder); return classBuilder.CreateType(); } @@ -1151,12 +1271,12 @@ namespace Spring.Util internal interface IFoo { - bool Spanglish( string foo, object[] args ); + bool Spanglish(string foo, object[] args); } internal sealed class ExplicitFoo : IFoo { - bool IFoo.Spanglish( string foo, object[] args ) + bool IFoo.Spanglish(string foo, object[] args) { return false; } @@ -1164,17 +1284,17 @@ namespace Spring.Util internal class ReflectionUtilsObject : IComparable { - public bool Spanglish( string foo, object[] args ) + public bool Spanglish(string foo, object[] args) { return false; } - public bool BadSpanglish( string foo ) + public bool BadSpanglish(string foo) { return false; } - public bool WickedSpanglish( string foo, string bar ) + public bool WickedSpanglish(string foo, string bar) { return false; } @@ -1184,7 +1304,7 @@ namespace Spring.Util /// /// /// - int IComparable.CompareTo( object obj ) + int IComparable.CompareTo(object obj) { return 0; } @@ -1192,7 +1312,7 @@ namespace Spring.Util internal sealed class ExtendedReflectionUtilsObject : ReflectionUtilsObject { - public void Declared( string name ) + public void Declared(string name) { } @@ -1211,17 +1331,17 @@ namespace Spring.Util /// internal class ReflectionUtilsObjectClone { - public bool Spanglish( string foo, object[] args ) + public bool Spanglish(string foo, object[] args) { return false; } - public bool BadSpanglish( string foo ) + public bool BadSpanglish(string foo) { return false; } - public bool WickedSpanglish( string foo, string bar ) + public bool WickedSpanglish(string foo, string bar) { return false; } @@ -1237,17 +1357,17 @@ namespace Spring.Util /// internal class ReflectionUtilsObjectBadClone { - public bool Spanglish( string foo, object[] args ) + public bool Spanglish(string foo, object[] args) { return false; } - public string BadSpanglish( string foo ) + public string BadSpanglish(string foo) { return foo; } - public bool WickedSpanglish( string foo, string bar ) + public bool WickedSpanglish(string foo, string bar) { return false; } @@ -1259,32 +1379,32 @@ namespace Spring.Util public string SelectedConstructor; - public GetMethodByArgumentValuesTarget( int flag, params object[] values ) + public GetMethodByArgumentValuesTarget(int flag, params object[] values) { SelectedConstructor = "ParamArrayMatch"; } - public GetMethodByArgumentValuesTarget( int flag, DummyArgumentType[] bars ) + public GetMethodByArgumentValuesTarget(int flag, DummyArgumentType[] bars) { SelectedConstructor = "ExactMatch"; } - public GetMethodByArgumentValuesTarget( int flag, ICollection bars ) + public GetMethodByArgumentValuesTarget(int flag, ICollection bars) { SelectedConstructor = "AssignableMatch"; } - public string MethodWithSimilarArguments( int flags, params object[] args ) + public string MethodWithSimilarArguments(int flags, params object[] args) { return "ParamArrayMatch"; } - public string MethodWithSimilarArguments( int flags, DummyArgumentType[] bars ) + public string MethodWithSimilarArguments(int flags, DummyArgumentType[] bars) { return "ExactMatch"; } - public string MethodWithSimilarArguments( int flags, ICollection bar ) + public string MethodWithSimilarArguments(int flags, ICollection bar) { return "AssignableMatch"; } @@ -1296,7 +1416,7 @@ namespace Spring.Util { } - public MyCustomAttribute( string name ) + public MyCustomAttribute(string name) { _name = name; } @@ -1314,7 +1434,7 @@ namespace Spring.Util /// public sealed class AnotherCustomAttribute : Attribute { - public AnotherCustomAttribute( string name, int age, bool hasSwallowedExplosives ) + public AnotherCustomAttribute(string name, int age, bool hasSwallowedExplosives) { _name = name; _age = age; @@ -1348,14 +1468,14 @@ namespace Spring.Util { private string[] favoriteQuotes = new string[10]; - [IndexerName( "MyItem" )] + [IndexerName("MyItem")] public string this[int index] { get { if (index < 0 || index >= favoriteQuotes.Length) { - throw new ArgumentException( "Index out of range" ); + throw new ArgumentException("Index out of range"); } return favoriteQuotes[index]; } @@ -1364,7 +1484,7 @@ namespace Spring.Util { if (index < 0 || index >= favoriteQuotes.Length) { - throw new ArgumentException( "index is out of range." ); + throw new ArgumentException("index is out of range."); } favoriteQuotes[index] = value; }