Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Util
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv9436
Modified Files:
ReflectionUtils.cs
Log Message:
SPRNET-852 - Creating a custom attribute did not take into account public field values that match the named arguments in the attribute declaration.
Index: ReflectionUtils.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Util/ReflectionUtils.cs,v
retrieving revision 1.55
retrieving revision 1.56
diff -C2 -d -r1.55 -r1.56
*** ReflectionUtils.cs 7 Dec 2007 17:59:20 -0000 1.55
--- ReflectionUtils.cs 24 Jan 2008 04:10:36 -0000 1.56
***************
*** 867,871 ****
object[] parameterValues = new object[attributeData.ConstructorArguments.Count];
Type[] parameterTypes = new Type[attributeData.ConstructorArguments.Count];
! object[] namedParameterValues = new object[attributeData.NamedArguments.Count];
// Fill arrays of the constructor parameters
--- 867,875 ----
object[] parameterValues = new object[attributeData.ConstructorArguments.Count];
Type[] parameterTypes = new Type[attributeData.ConstructorArguments.Count];
!
! IList namedParameterValues = new ArrayList();
! IList namedFieldValues = new ArrayList();
! //object[] namedParameterValues = new object[attributeData.NamedArguments.Count];
! //object[] namedFieldValues = new object[attributeData.NamedArguments.Count];
// Fill arrays of the constructor parameters
***************
*** 879,882 ****
--- 883,888 ----
PropertyInfo[] attributeProperties = attributeType.GetProperties(
BindingFlags.Instance | BindingFlags.Public);
+ FieldInfo[] attributeFields = attributeType.GetFields(
+ BindingFlags.Instance | BindingFlags.Public);
// Not using generics bellow as probably Spring.NET tries to keep
***************
*** 886,892 ****
--- 892,905 ----
IList propertiesToSet = new ArrayList();
int k = 0;
+
+ IList fieldsToSet = new ArrayList();
+ int n = 0;
+
+
// Fills arrays of the constructor named parameters
foreach (CustomAttributeNamedArgument namedArgument in attributeData.NamedArguments)
{
+ bool noMatchingProperty = false;
+
// Now iterate through all of the PropertyInfo, find the
// one with the corresponding to the NamedProperty name
***************
*** 897,901 ****
{
propertiesToSet.Add(attributeProperties[j]);
! namedParameterValues[k++] = ConvertValueIfNecessary(namedArgument.TypedValue.Value);
break;
}
--- 910,914 ----
{
propertiesToSet.Add(attributeProperties[j]);
! namedParameterValues.Add(ConvertValueIfNecessary(namedArgument.TypedValue.Value));
break;
}
***************
*** 905,908 ****
--- 918,923 ----
{
// In case of no match, throw
+ noMatchingProperty = true;
+ /*
throw new InvalidOperationException(
String.Format(CultureInfo.InvariantCulture,
***************
*** 911,914 ****
--- 926,954 ----
"on the attributeData {2}", namedArgument.MemberInfo.Name,
attributeType.FullName, attributeData));
+ */
+ }
+ }
+ }
+ if (noMatchingProperty)
+ {
+ for (int j = 0; j < attributeFields.Length; j++)
+ {
+ if (attributeFields[j].Name == namedArgument.MemberInfo.Name)
+ {
+ fieldsToSet.Add(attributeFields[j]);
+ namedFieldValues.Add(ConvertValueIfNecessary(namedArgument.TypedValue.Value));
+ break;
+ }
+ else
+ {
+ if (j == attributeFields.Length - 1)
+ {
+ throw new InvalidOperationException(
+ String.Format(CultureInfo.InvariantCulture,
+ "A property or public field with name {0} can't be found in the " +
+ "type {1}, but is present as a named property " +
+ "on the attributeData {2}", namedArgument.MemberInfo.Name,
+ attributeType.FullName, attributeData));
+ }
}
}
***************
*** 921,926 ****
propertiesToSet.CopyTo(namedProperties, 0);
! return new CustomAttributeBuilder(
! constructor, parameterValues, namedProperties, namedParameterValues);
}
--- 961,986 ----
propertiesToSet.CopyTo(namedProperties, 0);
! object[] propertyValues = new object[namedParameterValues.Count];
! namedParameterValues.CopyTo(propertyValues,0);
!
! if (fieldsToSet.Count == 0)
! {
! return new CustomAttributeBuilder(
! constructor, parameterValues, namedProperties, propertyValues);
! }
! else
! {
! FieldInfo[] namedFields = new FieldInfo[fieldsToSet.Count];
! fieldsToSet.CopyTo(namedFields, 0);
!
! object[] fieldValues = new object[namedFieldValues.Count];
! namedFieldValues.CopyTo(fieldValues, 0);
!
! return new CustomAttributeBuilder(
! constructor, parameterValues, namedProperties, propertyValues, namedFields, fieldValues);
! }
!
!
!
}
|