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" } );
精彩评论