Click here to Skip to main content
15,878,809 members
Articles / Silverlight
Alternative
Tip/Trick

Silverlight: Use KnownColor Through the Backdoor

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
5 Aug 2010CPOL 10.1K   1   6
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)


Written By
Retired
United Kingdom United Kingdom
Retired Systems Admin, Programmer, Dogsbody.
Mainly on Systems for Local Government, Health Authorities,
Insurance Industry - (COBOL eeeeeeeugh).
Inventor of Synchronized Shopping.

Comments and Discussions

 
GeneralWhoops missed the leading FF in Goldenrod Pin
Dean Wyant21-Sep-11 4:47
Dean Wyant21-Sep-11 4:47 
GeneralThere are colors missing - looks like G through L. For insta... Pin
Dean Wyant21-Sep-11 4:41
Dean Wyant21-Sep-11 4:41 
GeneralRe: Since my alternative doesn't contain a list of colours I can... Pin
Henry Minute21-Sep-11 5:26
Henry Minute21-Sep-11 5:26 
GeneralHey Henry, look at my 3rd edit. It's ultimately the best sol... Pin
#realJSOP12-Aug-10 3:49
mve#realJSOP12-Aug-10 3:49 
GeneralMay be not. As eventually someone has to iterate through tha... Pin
Rama Krishna Vavilala5-Aug-10 14:46
Rama Krishna Vavilala5-Aug-10 14:46 
GeneralLook at my 2nd edit. I think it will be faster than iteratin... Pin
#realJSOP5-Aug-10 14:36
mve#realJSOP5-Aug-10 14:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.