I have a class that has
ObservableCollection<int>
as a property, and I'm trying to change the value inside this property of an instance of this class. Here is the code that I have that gets a TargetException:
object[] index = null; var originalPropertyName = propertyName; if (propertyName.Contains("[") && propertyName.Contains("]")) { index = new object[1]; index[0] = Convert.ToInt32(propertyName.Split('[')[1].Split(']')[0]); propertyName = propertyName.Split('[')[0]; } PropertyInfo pi = item.GetType().GetProperty(propertyName); PropertyInfo opi = item.GetType().GetProperty(originalPropertyName); Type pType = index != null ? pi.PropertyType.GetGenericArguments()[0] : pi.PropertyType; if (pi != null) { object convertedValue = Convert.ChangeType(value, pType); if (index == null) { item.GetType().GetProperty(propertyName).SetValue(item, convertedValue, null); } else { //PropertyInfo ipi = pi.PropertyType.GetProperties().Single(p => p.GetIndexParameters().Length > 0); //var collection = pi.GetValue(item, index); //collection.GetType().GetProperty("Value").SetValue(collection, convertedValue, null); var _pi = pi.PropertyType.GetProperty("Item"); _pi.SetValue(pi, convertedValue, index); } }
As the property propertyName is not shown above, but in the case of an indexed property, it begins its life as "IndexedProperty [10]", for example.
In the comments after this "else", you can see other things that I tried by reading a few other stackoverflow posts and in other forums on how to do this, but still failed. Any ideas?
Casting a property to an ObservableCollection is not possible because I want it to be dynamic.
The concept of everything is to have a DataGrid connected to the data, and have a paste that works correctly by updating the correct properties of each instance, whether the properties are indexed or not. Non-indexed properties work fine, but I can't get the ObservableCollection to work.
reflection c #
Lefteris aslanoglou
source share