2

I'm working on an app in C# (Windows-Phone-7), and I'm trying to do something simple that has me stumped.

I want to cycle through each Color in Colors, and write out the Color name to a file (along with other stuff).

I have the simplest bit of code, which I know will not work, but I wrote to get started:

foreach (Color myColor in Colors)
{
}

Of course, this gives me the following syntax error:

'System.Windows.Media.Colors' is a 'type', but is used like a 'variable'.

Is there a way to do this? It seems really simple!

svick
  • 236,525
  • 50
  • 385
  • 514
pearcewg
  • 9,545
  • 21
  • 79
  • 125

3 Answers3

11

You can use this helper method to get a Dictionary of each Color's name/value pair.

public static Dictionary<string,object> GetStaticPropertyBag(Type t)
    {
        const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

        var map = new Dictionary<string, object>();
        foreach (var prop in t.GetProperties(flags))
        {
            map[prop.Name] = prop.GetValue(null, null);
        }
        return map;
    }

Use is with:

var colors = GetStaticPropertyBag(typeof(Colors));

foreach(KeyValuePair<string, object> colorPair in colors)
{
     Console.WriteLine(colorPair.Key);
     Color color = (Color) colorPair.Value;
}

Credit for the helper method goes to How can I get the name of a C# static class property using reflection?

Community
  • 1
  • 1
mfanto
  • 14,168
  • 6
  • 51
  • 61
6

You can use Reflection to get all of the properties within the Colors type:

var colorProperties = Colors.GetType().GetProperties(BindingFlags.Static | BindingFlags.Public);
var colors = colorProperties.Select(prop => (Color)prop.GetValue(null, null));
foreach(Color myColor in colors)
{
    // ....
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Maybe you should add a Where-clause to the end of your GetProperties (just in case), something like: `.GetProperties(...).Where(p => p.PropertyType == typeof(Color))` or something like that? – Alxandr Jan 29 '11 at 02:13
  • @Alxandr: This is safe as it is - no need for the extra check (in this case). All public properties of Colors are already of type Color. See: http://msdn.microsoft.com/en-us/library/system.windows.media.colors(VS.95).aspx – Reed Copsey Jan 29 '11 at 02:15
  • Ok, no prob then :-). Just remembering having something like that wrong once :-) – Alxandr Jan 29 '11 at 02:16
  • @Alxandr: Yeah - if it wasn't this specific case, it could be important. – Reed Copsey Jan 29 '11 at 02:17
  • Then you'd drop the casting in the select, and append the OfType? I've never used that one before. – Alxandr Jan 29 '11 at 02:18
  • @Alxandr: Yeah - you could do it that way. It's a LINQ-based filter by type. – Reed Copsey Jan 29 '11 at 02:19
  • 1
    This *seems* good, but when I try to use it (in WP7 at least), the Colors word gives me the following compiler error: An object reference is required for the nonstatic field, method, or property – pearcewg Jan 29 '11 at 02:25
2

Well you can do so by using this code.

     List<string> colors = new List<string>();

    foreach (string colorName in Enum.GetNames(typeof(KnownColor)))
    {
        //cast the colorName into a KnownColor
        KnownColor knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), colorName);
        //check if the knownColor variable is a System color
        if (knownColor > KnownColor.Transparent)
        {
            //add it to our list
            colors.Add(colorName);
        }
    }
Ali
  • 808
  • 2
  • 11
  • 20