mardi 20 janvier 2015

PropertyInfo.SetValue for an array is not working

I'm using .Net 2013, Windows7. I'm able to set and get individual values using reflection. When I try to use reflection to set a value in an array I'm unable to do so. Using reflection I'm able to get the values in an array.


I found what appears to be a workaround for setting the value in an array but I don't understand why the SetValue function doesn't work. Below is my code.



property = type.GetProperty("propDeviceMessageMask", BindingFlags.Public | BindingFlags.Instance);
if (property != null)
{
DEVICE_MESSAGE_MASK_Types[] value = (DEVICE_MESSAGE_MASK_Types[])property.GetValue(device, null);

value[0] = DEVICE_MESSAGE_MASK_Types.Disabled;
value[1] = DEVICE_MESSAGE_MASK_Types.Disabled;
value[2] = DEVICE_MESSAGE_MASK_Types.Disabled;
value[3] = DEVICE_MESSAGE_MASK_Types.Disabled;

property.SetValue(device, DEVICE_MESSAGE_MASK_Types.Disabled, new object[] { (int)4 });
}


propDeviceMessageMask is an array of 8 DEVICE_MESSAGE_MASK_Types. I'm able to get the entire array with the GetValue function. I'm able to edit the values using indexes 0-3. When I try to update index 4 with the SetValue function I get a Parameter Count Mismatch in a TargetParameterCountException.






Aucun commentaire:

Enregistrer un commentaire