简体   繁体   English

如何在C#紧凑框架中从颜色名称获取颜色?

[英]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. 但是我不知道如何从名称中获取颜色,因为紧凑框架中没有可用的Color.FromName()方法。

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. 我不确切知道紧凑框架支持什么,但是在“完整” C#中,PropertyInfo的每个实例都有一个属性“ Name”和一个方法“ GetValue”,您可以使用该方法查找仅给出名称的颜色。

Something like (if LINQ is allowed) 类似于(如果允许使用LINQ)

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

(Note that .Single throws an exception if colorName is not found.) (请注意,如果未找到colorName, .Single会引发异常。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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