Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Silverlight

Silverlight: Use KnownColor Through the Backdoor

5.00/5 (3 votes)
5 Aug 2010CPOL 11.1K  
A possible alternative solution. I say possible because I have no idea if it will work in Silverlight and even if it does whether it will be any easier/faster. public Color ColorFromName(string name) { Color result = Colors.Black; Type type =...
A possible alternative solution. I say possible because I have no idea if it will work in Silverlight and even if it does whether it will be any easier/faster.

C#
public Color ColorFromName(string name)
{
    Color result = Colors.Black;
    Type type = typeof(Brushes);
    foreach (PropertyInfo propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Static))
    {
        if (propertyInfo.PropertyType == typeof(SolidColorBrush))
        {
            if (propertyInfo.Name.ToLower() == name.ToLower())
            {
                result = ((SolidColorBrush)propertyInfo.GetValue(null, null)).Color;
                break;
            }
        }
    }

    return result;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)