G
G
Georgy Pelageikin2015-05-02 12:23:29
Arrays
Georgy Pelageikin, 2015-05-02 12:23:29

How to get the elements of an array or list through reflection (reflection, reflection)?

It seems like a simple question, but superficial Google and msdn did not answer the question, apparently I was looking badly. I am processing some object. And if it turned out to be an array, I need to recursively access all of its elements. The main problem is that it may not necessarily be an array, i.e. I cannot use the index operator [] with it.
Those. Simplified code looks like this:

...
if (obj.GetType().IsArray)
  {
    //Получить элементы массива obj
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Deerenaros, 2015-05-02
@ArXen42

I did not understand what "surface Google" means .

if (obj.GetType().IsArray)
  {
    obj.GetType().GetMethod("GetValue", typeof(int)).Invoke(obj, index);
  }

However, it might be easier to use the DLR .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question