简体   繁体   中英

How to get color from color name in C# compact framework?

I want to have a list of all colors and when i choose one color I need to get that color from it's name.

I am having list of colors by using this method...

    Type colorType = typeof(System.Drawing.Color);
                // We take only static property to avoid properties like Name, IsSystemColor ...
                PropertyInfo[] propInfos = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);

 foreach (var colorInfo in propInfos)
            {
// making a list here
}

but I don't know how to get color from it's name as there is no Color.FromName() method available in compact framework.

I don't know exactly what the compact framework supports, but in "full" C#, each instance of PropertyInfo has a property "Name" and a method "GetValue" that you could use to find the color given only the name.

Something like (if LINQ is allowed)

Color color = (Color)propInfos.Single(pi => pi.Name == colorName).GetValue(null);

(Note that .Single throws an exception if colorName is not found.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM