I have a PolygonRenderer class containing a Vertices property, which is a List, holding the points of the polygon the class renders.
When I try to change a specific point in this list by reflection, I get a System.Reflection.TargetParameterCountException on the last line of my function :
public override void ApplyValue(string property, object value, int? index)
{
List<PropertyInfo> properties = Data.GetType().GetProperties().ToList();
PropertyInfo pi = properties.FirstOrDefault(p => p.Name == property);
pi.SetValue(Data, value,
index.HasValue ? new object[] { index.Value } : null);
}
When I debug, I get index.Value = 3, Data is the PolygonRenderer instance and pi reflects the Vertices property, which count = 4.
Since my index is supposed to be the last item of my list, how is it possible that I get a count exception on that property ?
Thanks
I have a PolygonRenderer class containing a Vertices property, which is a List...
So you need to execute something like this
Data.Vertices[index] = value
and what your code is trying to do is
Data[index] = value
You can use something like this instead
public override void ApplyValue(string property, object value, int? index)
{
object target = Data;
var pi = target.GetType().GetProperty(property);
if (index.HasValue && pi.GetIndexParameters().Length != 1)
{
target = pi.GetValue(target, null);
pi = target.GetType().GetProperties()
.First(p => p.GetIndexParameters().Length == 1
&& p.GetIndexParameters()[0].ParameterType == typeof(int));
}
pi.SetValue(target, value, index.HasValue ? new object[] { index.Value } : null);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With