开发者

PropertyInfo.GetValue() how do I index collection by string using reflection in C#?

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

pu开发者_运维知识库blic 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?


NameValueCollection coll;
var indexer = typeof(NameValueCollection).GetProperty(
    "Item",
    new[] { typeof(string) }
);
var item = indexer.GetValue(coll, new [] { "key" } );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜