比如一个类:
public test
{
   public string this[int index]
   { 
       get {
         return "test";
       }
   }
}正常取值是 test t = new test();
string x = t[0];
我怎么通过反射去获取,我不知道这个东西是不是叫 “Attribute”,求高手解答!有定给分

解决方案 »

  1.   


                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);
                }
      

  2.   

    test字符串不是固定了么一般都是有个容器的
      

  3.   


    即然用反射,说明类型是未知的二楼的回答其实是这样的 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);
                }
      

  4.   

    我这里是打个比方,比如对象也可能是哈希表,也可能是datatable ,还可能是任何自定义的类,不过datatable 的话,这么取值,好像有问题,因为满足type.GetProperty("Item")的不止一个,不过我通过其它方法解法了,感谢二楼回复