c# - 反射 PropertyInfo.GetValue 来自集合

标签 c# .net reflection collections propertyinfo

我在反射、动态调用对象和读取集合值方面遇到问题。
在引用的 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) >)(在对象浏览器中不可见/不存在)

最佳答案

您尝试过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'" } );

关于c# - 反射 PropertyInfo.GetValue 来自集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3462365/

相关文章:

java - 使用反射传递 Double 类型的参数

java - 无论如何我可以将 Map 转换为 POJO 然后我可以在 JSP EL 中使用吗?

c# - 如何优化此代码以获得更好的可读性?

c# - LINQ to SQL 可以生成包含 ISNULL 函数的 TSQL 吗?

c# - 在修改与 NHibernate 的多对多关系时防止删除/插入

.net - 防止 IIS 在任务结束前杀死它

c# - .NET Itemtemplate 转发器?

.NET Framework 和版本兼容性

c# - c#中反射中的SetValue

c# - FormattedText.BuildGeometry 删除字符