Question

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)

Was it helpful?

Solution

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'" } );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top