Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Reflection/Dynamic
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv20549/src/Spring/Spring.Core/Reflection/Dynamic
Modified Files:
DynamicProperty.cs DynamicReflectionManager.cs
Log Message:
additional tests and minor fixes to SafeProperty and DynamicProperty
Index: DynamicReflectionManager.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Reflection/Dynamic/DynamicReflectionManager.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** DynamicReflectionManager.cs 16 May 2008 10:02:39 -0000 1.4
--- DynamicReflectionManager.cs 17 May 2008 11:05:26 -0000 1.5
***************
*** 393,397 ****
ILGenerator il = dm.GetILGenerator();
! if (propertyInfo.CanWrite)
{
MethodInfo method = propertyInfo.GetSetMethod(true);
--- 393,398 ----
ILGenerator il = dm.GetILGenerator();
! if (propertyInfo.CanWrite
! && !(propertyInfo.DeclaringType.IsValueType && !propertyInfo.GetSetMethod(true).IsStatic))
{
MethodInfo method = propertyInfo.GetSetMethod(true);
Index: DynamicProperty.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Reflection/Dynamic/DynamicProperty.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** DynamicProperty.cs 13 May 2008 23:23:03 -0000 1.2
--- DynamicProperty.cs 17 May 2008 11:05:26 -0000 1.3
***************
*** 170,173 ****
--- 170,174 ----
private readonly bool isOptimizedGet = false;
private bool isOptimizedSet = false;
+ private readonly bool canSet;
/// <summary>
***************
*** 186,191 ****
isOptimizedGet = true;
}
! if (property.CanWrite
! && property.GetSetMethod() != null
&& ReflectionUtils.IsTypeVisible(property.DeclaringType, DynamicReflectionManager.ASSEMBLY_NAME))
{
--- 187,193 ----
isOptimizedGet = true;
}
!
! canSet = property.CanWrite && !(propertyInfo.DeclaringType.IsValueType && !propertyInfo.GetSetMethod(true).IsStatic);
! if (property.GetSetMethod() != null
&& ReflectionUtils.IsTypeVisible(property.DeclaringType, DynamicReflectionManager.ASSEMBLY_NAME))
{
***************
*** 246,249 ****
--- 248,258 ----
else
{
+ if (!canSet)
+ {
+ throw (propertyInfo.DeclaringType.IsValueType)
+ ? new InvalidOperationException("Cannot set property value on a value type due to boxing.")
+ : new InvalidOperationException("Cannot set value of a read-only property.");
+ }
+
propertyInfo.SetValue(target, value, null);
}
***************
*** 272,275 ****
--- 281,322 ----
#endregion
+ #if NET_2_0
+ /// <summary>
+ /// Factory class for dynamic properties.
+ /// </summary>
+ /// <author>Aleksandar Seovic</author>
+ /// <version>$Id$</version>
+ public class DynamicProperty : BaseDynamicMember
+ {
+ /// <summary>
+ /// Creates safe dynamic property instance for the specified <see cref="PropertyInfo"/>.
+ /// </summary>
+ /// <remarks>
+ /// <p>This factory method will create a dynamic property with a "safe" wrapper.</p>
+ /// <p>Safe wrapper will attempt to use generated dynamic property if possible,
+ /// but it will fall back to standard reflection if necessary.</p>
+ /// </remarks>
+ /// <param name="property">Property info to create dynamic property for.</param>
+ /// <returns>Safe dynamic property for the specified <see cref="PropertyInfo"/>.</returns>
+ /// <seealso cref="SafeProperty"/>
+ public static IDynamicProperty CreateSafe(PropertyInfo property)
+ {
+ return new SafeProperty(property);
+ }
+
+ /// <summary>
+ /// Creates dynamic property instance for the specified <see cref="PropertyInfo"/>.
+ /// </summary>
+ /// <param name="property">Property info to create dynamic property for.</param>
+ /// <returns>Dynamic property for the specified <see cref="PropertyInfo"/>.</returns>
+ public static IDynamicProperty Create(PropertyInfo property)
+ {
+ AssertUtils.ArgumentNotNull(property, "You cannot create a dynamic property for a null value.");
+
+ IDynamicProperty dynamicProperty = new SafeProperty(property);
+ return dynamicProperty;
+ }
+ }
+ #else
/// <summary>
/// Factory class for dynamic properties.
***************
*** 369,373 ****
{
bool isValueType = property.DeclaringType.IsValueType;
! if (isValueType)
{
ThrowInvalidOperationException(il, "Cannot set property value on a value type due to boxing.");
--- 416,422 ----
{
bool isValueType = property.DeclaringType.IsValueType;
! // we can't set nonstatic properties on value types (due to boxing)
! bool canSet = !(isValueType && !property.GetSetMethod(true).IsStatic);
! if (!canSet)
{
ThrowInvalidOperationException(il, "Cannot set property value on a value type due to boxing.");
***************
*** 384,388 ****
SetupArgument(il, property.PropertyType, 2);
! InvokeMethod(il, isStatic, isValueType, setMethod);
il.Emit(OpCodes.Ret);
}
--- 433,437 ----
SetupArgument(il, property.PropertyType, 2);
! InvokeMethod(il, isStatic, property.DeclaringType.IsValueType, setMethod);
il.Emit(OpCodes.Ret);
}
***************
*** 396,398 ****
--- 445,449 ----
#endregion
}
+
+ #endif
}
\ No newline at end of file
|