Reflection.TargetException problem
I want the EmployeeNumber property for an Employee to only be set by the database, but I need to be able to set the value for unit testing. Which is why I made the helper code below.
Can someone help me troubleshoot & fix this?
Cheers,
BerrylHelper Code
public static void SetEmployeeNumberFor(Employee employee, EmployeeNumber employeeNumber)
{
var empNumberProperty = employee.GetType().GetProperty("EmployeeNumber", BindingFlags.Public | BindingFlags.Instance);
Check.Invariant(empNumberProperty != null);
Check.Invariant(empNumberProperty.PropertyType.Equals(typeof(EmployeeNumber)));
empNumberProperty.SetValue(empNumberProperty, employeeNumber, null);
}
Full Exception
System.Reflection.TargetException : Object does not match target type.
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.Invoke(Object开发者_运维百科 obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
TestableEmployee.cs(36,0): at Smack.ConstructionAdmin.TestingSupport.ResourceHelper.SetEmployeeNumberFor(Employee employee, EmployeeNumber employeeNumber)
TestableEmployee.cs(47,0): at Smack.ConstructionAdmin.TestingSupport.ResourceHelperTests.SetEmployeeNumberFor()
EmployeeNumber property code
public virtual EmployeeNumber EmployeeNumber {
get { return _employeeNumber; }
protected set {
_employeeNumber = value;
//base.BusinessId = _employeeNumber;
}
}
private EmployeeNumber _employeeNumber;
Try using this instead:
empNumberProperty.SetValue(employee, employeeNumber, null);
The issue is that the first parameter of SetValue is the object that your trying to modify.
While looking at the MSDN Documentation, I also noticed that this method can throw a MethodAccessException
if "there was an illegal attempt to access a private or protected method inside a class" which seems like it would apply in this situation. In the remarks section, this notice appears:
Starting with the .NET Framework version 2.0 Service Pack 1, this method can be used to access non-public members if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the non-public members is restricted to the caller’s grant set, or a subset thereof. To use this functionality, your application should target the .NET Framework version 3.5 or later.
Alternatively, you could try using GetSetMethod(bool)
, but that would still be subject to the same security restrictions.
empNumberProperty.GetSetMethod(true).Invoke(employee, new[] { employeeNumber });
empNumberProperty.SetValue(empNumberProperty, employeeNumber, null);
Should be
empNumberProperty.SetValue(employee, employeeNumber, null);
精彩评论