locked
SetValue of of a List<int16> property using reflection. RRS feed

  • Question

  • 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!

    Friday, May 11, 2012 11:55 AM

Answers

All replies