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

时间:2012-12-27 10:04:59

标签: c# reflection

我有一个有

的课程
ObservableCollection<int>

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

如何获得propertyName不会在上面显示,但在索引属性的情况下,它的开头就是“IndexedProperty [10]”。例如。

在“else”之后的评论中你可以通过阅读其他一些stackoverflow帖子和其他论坛上的内容来看到我尝试过的其他内容,但是到目前为止我还是失败了。有什么想法吗?

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

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

2 个答案:

答案 0 :(得分:13)

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

基本上,你需要记住:

foo.People[10] = new Person();

相当于:

var people = foo.People; // Getter
people[10] = new Person(); // Indexed setter

看起来你已经几乎了这个注释掉的代码:

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

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

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

答案 1 :(得分:0)

试试这个,我不确定它是否会起作用

_pi.SetValue(pi, convertedValue, new object[] { (int) 0 }); 

//where 0 is the index in which you want to insert the value, in this case to index 0