views:

28

answers:

1

Hi Guys.

Let's say I have a class, which has a NameValueCollection property.

public class TestClass
{
    public NameValueCollection Values { get; private set; }

    public TestClass()
    {
        Values = new NameValueCOllection();
        Values.Add("key", "value");
        Values.Add("key1", "value1");
    }
}

I know how to get items of Values collection using int indexer (GetProperty() and GetValue() functions can do it). But how can I get item of this NameValueCollection by string key using .net reflection?

+1  A: 
NameValueCollection coll;
var indexer = typeof(NameValueCollection).GetProperty(
    "Item",
    new[] { typeof(string) }
);
var item = indexer.GetValue(coll, new [] { "key" } );
Jason
Thank's alot, I missed that 2-nd param of GetValue function is object[] :)
deff