ReeCube
ReeCube

Reputation: 2597

Static class memeber to array

How can I get an array with all static Strings of a class inside?

What I have:

public static class A
{
    public static class BA
    {
        public const string BAA = "baa";
        public const string BAB = "bab";
        public const string BAC = "bac";
    }
    public static class BB
    {
        public const string BBA = "bba";
        public const string BBB = "bbb";
        public const string BBC = "bbc";
    }
}

What I want:

string[] BAs = new string[]{"baa","bab","bac"};
string[] BBs = new string[]{"bba","bbb","bbc"};

Would be nice too (but prefer solution above):

string[] BAs = new string[]{"BAA","BAB","BAC"};
string[] BBs = new string[]{"BBA","BBB","BBC"};

Some pseudo code:

string[] BAs = A.BA.GetAllStringVariablesFromStaticMembers();
string[] BBs = A.BB.GetAllStringVariablesFromStaticMembers();

Is this possible at all? And if it is, how can I do it?

Additional information: The static classes just contain static strings inside, so a general member method is possible too, but I prefer if the method returns just the static strings of the given class.

Upvotes: 1

Views: 61

Answers (3)

public static class TypeExtension
{
    public static string[] GetStaticStrings(this Type type)
    {
        return type
                 .GetFields(BindingFlags.Public | BindingFlags.Static)
                 .Where(field => field.FieldType == typeof(string))
                 .Select(field => (string)field.GetValue(null))
                 .ToArray();
    }
}

Usage:

string[] arr = typeof(A.BA).GetStaticStrings();

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You can do this via Reflection with a slightly different syntax:

public static String[] GetAllStringVariablesFromStaticMembers(Type value) {
  if (Object.ReferenceEquals(null, value))
    throw new ArgumentNullException("value");

  List<String> result = new List<String>();

  foreach (FieldInfo fi in value.GetFields(BindingFlags.Static | BindingFlags.Public)) 
    if (fi.FieldType == typeof(String))
      result.Add(fi.GetValue(null) as String);

  return result.ToArray();
}

...

string[] BAs = GetAllStringVariablesFromStaticMembers(typeof(A.BA)); 

Upvotes: 3

Maarten
Maarten

Reputation: 22945

I think this will do what you require. Basically you need an instance of a class to do A.BA since BA will either be a property of a field which will point to an instance.

public static class A {
    public static readonly A_Wrapper BA = new A_Wrapper(
        new string[3] {
            "baa",
            "bab",
            "bac"
        }
    );

        public static readonly A_Wrapper BB = new A_Wrapper(
        new string[3] {
            "bba",
            "bbb",
            "bbc"
        }
    );

    public class A_Wrapper {
        public A_Wrapper(string[] data) {
            this.data = data;
        }
        private readonly string[] data;
        public string[] GetAllStringVariablesFromStaticMembers() {
            return data;
        }
    }
}

var data1 = A.BA.GetAllStringVariablesFromStaticMembers(); // baa, bab, bac
var data2 = A.BB.GetAllStringVariablesFromStaticMembers(); // bba, bbb, bbc

Upvotes: 0

Related Questions