fixed failing DynamicField Test
This commit is contained in:
@@ -249,26 +249,31 @@ namespace Spring.Reflection.Dynamic
|
||||
try
|
||||
{
|
||||
dynamicField.SetValue(target, value);
|
||||
return;
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
isOptimizedSet = false;
|
||||
if (!canSet)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot set value of a read-only field or a constant.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fieldInfo.SetValue(target, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (!canSet)
|
||||
{
|
||||
if (!canSet)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot set value of a read-only field or a constant.");
|
||||
}
|
||||
fieldInfo.SetValue(target, value);
|
||||
throw new InvalidOperationException("Cannot set value of a read-only field or a constant.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
fieldInfo.SetValue(target, value);
|
||||
}
|
||||
catch(ArgumentException)
|
||||
{
|
||||
if (value != null && !fieldInfo.GetType().IsAssignableFrom(value.GetType()))
|
||||
{
|
||||
throw new InvalidCastException();
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
internal FieldInfo FieldInfo
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace Spring.Reflection.Dynamic
|
||||
[TestFixture]
|
||||
public class DynamicFieldTests
|
||||
{
|
||||
private const BindingFlags BINDANY = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
|
||||
protected const BindingFlags BINDANY = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
|
||||
protected Inventor tesla;
|
||||
protected Inventor pupin;
|
||||
protected Society ieee;
|
||||
@@ -160,22 +160,6 @@ namespace Spring.Reflection.Dynamic
|
||||
Assert.AreEqual( 2008, myField.GetValue( null ) );
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSetIncompatibleType()
|
||||
{
|
||||
IDynamicField inventorPlace = Create( typeof( Inventor ).GetField( "pob", BINDANY ) );
|
||||
try { inventorPlace.SetValue( new Inventor(), new object() ); Assert.Fail(); }
|
||||
catch (InvalidCastException) { }
|
||||
try { inventorPlace.SetValue( new Inventor(), new DateTime() ); Assert.Fail(); }
|
||||
catch (InvalidCastException) { }
|
||||
|
||||
IDynamicField inventorDOB = Create( typeof( Inventor ).GetField( "dob", BINDANY ) );
|
||||
try { inventorDOB.SetValue( new Inventor(), 2 ); Assert.Fail(); }
|
||||
catch (InvalidCastException) { }
|
||||
try { inventorDOB.SetValue( new Inventor(), new Place() ); Assert.Fail(); }
|
||||
catch (InvalidCastException) { }
|
||||
}
|
||||
|
||||
#region Performance tests
|
||||
|
||||
private DateTime start, stop;
|
||||
|
||||
@@ -70,6 +70,22 @@ namespace Spring.Reflection.Dynamic
|
||||
TestStaticMembersOf(typeof(MyClassWithPrivateFields), MyClassWithPrivateFields.GetStaticReadonlyRefValue());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestSetIncompatibleType()
|
||||
{
|
||||
IDynamicField inventorPlace = Create( typeof( Inventor ).GetField( "pob", BINDANY ) );
|
||||
try { inventorPlace.SetValue( new Inventor(), new object() ); Assert.Fail(); }
|
||||
catch (InvalidCastException) { }
|
||||
try { inventorPlace.SetValue( new Inventor(), new DateTime() ); Assert.Fail(); }
|
||||
catch (InvalidCastException) { }
|
||||
|
||||
IDynamicField inventorDOB = Create( typeof( Inventor ).GetField( "dob", BINDANY ) );
|
||||
try { inventorDOB.SetValue( new Inventor(), 2 ); Assert.Fail(); }
|
||||
catch (InvalidCastException) { }
|
||||
try { inventorDOB.SetValue( new Inventor(), new Place() ); Assert.Fail(); }
|
||||
catch (InvalidCastException) { }
|
||||
}
|
||||
|
||||
private void TestStaticMembersOf(Type type, object expectedRoRefValue)
|
||||
{
|
||||
// ro const int
|
||||
|
||||
Reference in New Issue
Block a user