使用Reflection的C#中索引属性的SetValue

Lef*_*lou 10 c# reflection

我有一个班级有一个

ObservableCollection<int>
Run Code Online (Sandbox Code Playgroud)

作为属性,我正在尝试更改该类实例的属性内的值.这是我的代码,并且得到了一个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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何获得propertyName没有在上面显示,但在索引属性的情况下,它的生命开始为例如"IndexedProperty [10]".

在"其他"之后的评论中,您可以通过阅读其他一些stackoverflow帖子以及其他论坛上的内容来查看我尝试过的其他内容,但是到目前为止我已经失败了.有任何想法吗?

将属性转换为ObservableCollection是不可行的,因为我希望它是动态的.

整个事情的概念是通过更新每个实例的适当属性来使DataGrid具有数据绑定并使粘贴正常工作,无论属性是否已编入索引.非索引属性工作正常,但我无法使ObservableCollection工作.

Jon*_*eet 13

具有ObservableCollection<int>作为属性的类实际上并不具有传统意义上的索引器的索引属性.它只有一个非索引属性,它本身有一个索引器.所以你需要使用GetValue开始(不指定索引),然后在结果上获取索引器.

基本上,你需要记住:

foo.People[10] = new Person();
Run Code Online (Sandbox Code Playgroud)

相当于:

var people = foo.People; // Getter
people[10] = new Person(); // Indexed setter
Run Code Online (Sandbox Code Playgroud)

看起来你几乎就在这里注释了这段代码:

//var collection = pi.GetValue(item, index);
//collection.GetType().GetProperty("Value").SetValue(collection, convertedValue, null);
Run Code Online (Sandbox Code Playgroud)

...但你在错误的点上应用索引.你想要(我认为 - 这个问题并不十分明确):

var collection = pi.GetValue(item, null);
collection.GetType()
          .GetProperty("Item") // Item is the normal name for an indexer
          .SetValue(collection, convertedValue, index);
Run Code Online (Sandbox Code Playgroud)