反射 PropertyInfo.GetValue 来自集合

发布于 2024-09-13 22:14:15 字数 830 浏览 4 评论 0原文

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

I have problem with reflection, dynamic invoking objects and reading collection values.
In Referenced COM/Interop it would look like this:

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

Unfortunetly i need to make it with reflection and dynamic invoking object. Getting collection is rather easy, but reading "obj" is different story. How should i wrote this?

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, ???);

I should add that in object browser i see "this[v object]", but in ICollection.GetMethods() i'm getting property Item (System.Object) (which is invisible/not there in Object Browser)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2024-09-20 22:14:15

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

Have you tried 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'" } );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文