.net 如何使用C#中的反射按字符串索引集合?
假设我有一个类,它有一个NameValueCollection属性.net 如何使用C#中的反射按字符串索引集合?,.net,reflection,collections,indexing,.net,Reflection,Collections,Indexing,假设我有一个类,它有一个NameValueCollection属性 public class TestClass { public NameValueCollection Values { get; private set; } public TestClass() { Values = new NameValueCOllection(); Values.Add("key", "value"); Values.Add("k
public class TestClass
{
public NameValueCollection Values { get; private set; }
public TestClass()
{
Values = new NameValueCOllection();
Values.Add("key", "value");
Values.Add("key1", "value1");
}
}
我知道如何使用int indexer获取值集合项(GetProperty()和GetValue()函数可以做到这一点)。但如何使用.net反射通过字符串键获取此NameValueCollection的项?为什么要通过反射来获取此项?非常感谢,我错过了GetValue函数的第2个参数object[]:)
NameValueCollection coll;
var indexer = typeof(NameValueCollection).GetProperty(
"Item",
new[] { typeof(string) }
);
var item = indexer.GetValue(coll, new [] { "key" } );