1

在 Windows.Forms 中,我需要创建一个接受任何颜色的程序并尝试为其找到相应的系统颜色。

我无法弄清楚如何遍历 System.Drawing.SystemColors 类的所有颜色 - 它是一个类,而不是枚举或列表。

我该怎么做(某种反思?)?

4

2 回答 2

3

怎么样

public static Dictionary<string,object> GetStaticPropertyBag(Type t)
{
    const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

    var map = new Dictionary<string, object>();
    foreach (var prop in t.GetProperties(flags))
    {
        map[prop.Name] = prop.GetValue(null, null);
    }
    return map;
}

或者

foreach (System.Reflection.PropertyInfo prop in typeof(SystemColors).GetProperties())
{
     if (prop.PropertyType.FullName == "System.Drawing.Color")
         ColorComboBox.Items.Add(prop.Name);
}
于 2012-09-08T08:20:12.443 回答
1

我煮了一些东西。

var typeToCheckTo = typeof(System.Drawing.Color);
var type = typeof(System.Drawing.SystemColors);
var fields = type.GetProperties(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).Where(p => p.PropertyType.Equals(typeToCheckTo));
foreach (var field in fields)
{
    Console.WriteLine(field.Name + field.GetValue(null, null));
}
于 2012-09-08T08:25:12.020 回答