比如一个类:
public test
{
public string this[int index]
{
get {
return "test";
}
}
}正常取值是 test t = new test();
string x = t[0];
我怎么通过反射去获取,我不知道这个东西是不是叫 “Attribute”,求高手解答!有定给分
public test
{
public string this[int index]
{
get {
return "test";
}
}
}正常取值是 test t = new test();
string x = t[0];
我怎么通过反射去获取,我不知道这个东西是不是叫 “Attribute”,求高手解答!有定给分
test t=new test();
Type type = typeof(test);
PropertyInfo pi = type.GetProperty("Item");
if (pi != null)
{
object obj = pi.GetValue(t, new object[] { 0 });
Console.WriteLine(obj);
}
即然用反射,说明类型是未知的二楼的回答其实是这样的 object t =new test();
Type type = t.GetType();
PropertyInfo pi = type.GetProperty("Item");
if (pi != null)
{
object obj = pi.GetValue(t, new object[] { "参数" });
Console.WriteLine(obj);
}