来自Collection的反射PropertyInfo.GetValue

时间:2010-08-11 20:08:11

标签: c# .net reflection collections propertyinfo

我遇到反射,动态调用对象和读取集合值的问题 在Referenced COM / Interop中,它看起来像这样:

ICollection collection = (ICollection)sth.getCollection("parameter");
SomeObject obj = (SomeObject)collection["id='1'"]; //DB WHERE condition

不幸的是,我需要使用反射和动态调用对象来实现它。收集相当容易,但阅读“obj”是不同的故事。我该怎么写这个?

object oICollection = sthGetCollectionMethod.Invoke(
    sthInstance, BindingFlags.Instance | BindingFlags.Public, null,
    new object[1] { "parameter" },
    System.Globalization.CultureInfo.InvariantCulture);
//and here is the problem:
//how to access object as array/hashtable collection?
object obj = tICollection.GetProperty("???").GetValue(oICollection, ???);

我应该在对象浏览器中添加,我看到“this [v object]”,但在ICollection.GetMethods()我正在获取属性Item(System.Object)(在对象浏览器中不可见/不存在)

1 个答案:

答案 0 :(得分:4)

您是否尝试过get_Item

object oICollection = sthGetCollectionMethod.Invoke(
    sthInstance, BindingFlags.Instance | BindingFlags.Public, null,
    new object[1] { "parameter" },
    System.Globalization.CultureInfo.InvariantCulture);

object obj = tICollection.GetMethod("get_Item").Invoke(
    oICollection, new object[] { "id='1'" } );