string A = "a";foreach(System.Reflection.PropertyInfo property in A.GetType().GetProperties())
{
System.Console.WriteLine(property.GetValue(A,null));
}运行到property.GetValue(A,null)这里会报错,TargetParameterCountException,不知道是为什么。谢谢啦

解决方案 »

  1.   

    因为属性Chars是索引器,需要提供索引,才可以提取数据。
    string a = "ABC";
    PropertyInfo p = a.GetType().GetProperty( "Length" );
    if( p != null )
    {
    Console.WriteLine( p.GetValue( a,null ) );
    }
    可以。值为3
      

  2.   

    property.GetValue(A,new object[0])
      

  3.   

    有没有办法判断这个property是不是索引器?
      

  4.   

    if (property.GetIndexParameters().Length == 0)
    {
    System.Console.WriteLine(property.GetValue(A,null));
    }
    else
    {
    for(int i=0; i < property.GetIndexParameters().Length; i++)
    {
    System.Console.WriteLine(property.GetValue(A,new object[]{i}));
    }
    }gao ding le
      

  5.   

    通过PropertyInfo来判断,索引器的Name为Item,是固定的。
    例如:
    public class PropertyTest
    {
    private int[] nTest;
    public int this[int Index]
    {
    get{return nTest[Index];}
    set{nTest[Index] =value;}
    }
    }
    //Get properties
    Type typ = typeof( PropertyTest );
    foreach( PropertyInfo pi in typ.GetProperties( ) )
    {
    if( pi.Name == "Item" )
    {
    Debug.WriteLine( "Index property" );
    }
    }
      

  6.   

    至于获值与GetIndexParameters并无太大关系。
      

  7.   

    请教一下 有没有办法取得这个index里面的item个数