在 Windows.Forms 中,我需要创建一个接受任何颜色并尝试找到与之对应的系统颜色的程序。
我无法弄清楚如何遍历 System.Drawing.SystemColors 类的所有颜色 - 它是一个类,而不是枚举或列表。
我该怎么做(某种反射?)?
最佳答案
怎么样
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);
}
关于c# - 列出所有系统颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12329138/