65.9K
CodeProject is changing. Read more.
Home

Silverlight: Use KnownColor Through the Backdoor

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Aug 5, 2010

CPOL
viewsIcon

11095

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.
        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;
        }