18
FieldInfo[] fields = typeof(MyDictionary).GetFields();

MyDictionary is a static class, all fields are string arrays.

How to get get the Length value of each array and then itearate through all elements ? I tried the cast like:

field as Array

but it causes an error

Cannot convert type 'System.Reflection.FieldInfo' to 'System.Array' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

2
  • what exactly you want to do ? Commented Jul 7, 2010 at 11:37
  • I need to iterate through all array's elements to check if some values exists in any array Commented Jul 7, 2010 at 11:39

3 Answers 3

15

As an example:

using System;
using System.Reflection;

namespace ConsoleApplication1
{
    public static class MyDictionary
    {
        public static int[] intArray = new int[] { 0, 1, 2 };
        public static string[] stringArray = new string[] { "zero", "one", "two" };
    }

    static class Program
    {
        static void Main(string[] args)
        {
            FieldInfo[] fields = typeof(MyDictionary).GetFields();

            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsArray)
                {
                    Array array = field.GetValue(null) as Array;

                    Console.WriteLine("Type: " + array.GetType().GetElementType().ToString());
                    Console.WriteLine("Length: " + array.Length.ToString());
                    Console.WriteLine("Values");
                    Console.WriteLine("------");

                    foreach (var element in array)
                        Console.WriteLine(element.ToString());
                }

                Console.WriteLine();
            }

            Console.Readline();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

12

Edit after your edit: Note that what you have is reflection objects, not objects or values related to your own class. In other words, those FieldInfo objects you have there are common to all instances of your class. The only way to get to the string arrays is to use those FieldInfo objects to get to the field value of a particular instance of your class.

For that, you use FieldInfo.GetValue. It returns the value of the field as an object.

Since you already know they are string arrays, that simplifies things:

If the fields are static, pass null for the obj parameter below.

foreach (var fi in fields)
{
    string[] arr = (string[])fi.GetValue(obj);
    ... process array as normal here
}

If you want to ensure you only process fields with string arrays:

foreach (var fi in fields)
{
    if (fi.FieldType == typeof(string[]))
    {
        string[] arr = (string[])fi.GetValue(obj);
        ... process array as normal here
    }
}

3 Comments

OK, but in this case I pressume the 'obj' would be an instance of the MyDictionary class, but I can't make an instance of that class because it's a static class
Just pass null for obj, I didn't read your question thoroughly enough :)
@Tony just pass null instead of obj
4

Like this:

FieldInfo[] fields = typeof(MyDictionary).GetFields();
foreach (FieldInfo info in fields) {
  string[] item = (string[])info.GetValue(null);
  Console.WriteLine("Array contains {0} items:", item.Length);
  foreach (string s in item) {
    Console.WriteLine("  " + s);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.