开发者

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

开发者 https://www.devze.com 2023-01-21 14:06 出处:网络
Let\'s say I have a class, which has a NameValueCollection property. pu开发者_运维知识库blic class TestClass

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

精彩评论

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