SetValue of of a List<int16> property using reflection.
-
Friday, May 11, 2012 11:55 AM
I have a List<int16> property and I am trying to set the data in an index of the list using reflection.
The Property I am trying to change:
public List<Int16> ListOfInts
{
get
{
return _ListOfInts;
}
set
{
_ListOfInts = value;
}
}I have initialised _ListOfInts and added 2 ints e.g
_ListOfInts = new List<int16>();
_ListOfInts.add(0);
_ListOfInts.add(0);My code:
PropertyInfo propertyToChange = comp.GetType().GetProperty("ListOfInts");
propertyToChange.SetValue(comp, data, new object[] { (int)0 });I am trying to set the data in index 0 of the property ListOfInts to the value in data.
When I run the code I get the exception: Parameter Count Mismatch. I have checked that the list has been initialised and it contains 2 indexes.
I have run the similar code using a non list and it works, so its just the bit which sets the index that isn't working.
PropertyInfo propertyToChange = comp.GetType().GetProperty("ListOfInts");
propertyToChange.SetValue(comp, data, null);All help appreciated! Thanks in advance!
All Replies
-
Friday, May 11, 2012 12:04 PM
That code works for an indexed property.
Get the property instance using the method Type.GetProperty().GetValue(), than cast it to a List<short> and set the value normally.Matteo Migliore
Bloghttp://blogs.ugidotnet.org/matteomigliore
Twitterhttp://twitter.com/matteomigliore
CodePlex- Marked As Answer by J-Eng Friday, May 11, 2012 12:27 PM
-
Friday, May 11, 2012 12:18 PM
Hi,
Check this:
Type tp = _ListOfInts .GetType(); System.Reflection.PropertyInfo pInf = tp.GetProperty("Item"); int value = 3; int index = 0; pInf.SetValue(intList, value, new object[] { index });
Bilhan silva
-
Friday, May 11, 2012 12:27 PM
Thanks for that!

