views:

67

answers:

1

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)

+3  A: 

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'" } );
mathieu
Ought to be close. Or GetProperty("Item").
Hans Passant
Thx, Seems to be working ok, but i have other problem now thanks to this *** reflection :(
cichy