reflection on List and printing values

前端 未结 6 870
眼角桃花
眼角桃花 2020-12-20 18:31

I wrote a method that accepts a generic parameter and then it prints its properties. I use it to test my web service. It\'s working but I want to add some features that I do

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 19:11

    The elements inside a list can be retrieved through the indexer property Item. This property accepts an index argument (there is an overload of PropertyInfo.GetValue that accept an object array, just like MethodInfo.Invoke) and returns the object at that position.

    int index = /* the index you want to get here */;
    PropertyInfo indexer = Object.GetProperty("Item");
    object item = indexer.GetValue(Object, new object[] { index });
    

提交回复
热议问题