在Windows.Forms中,我需要创建一个可以接受任何颜色的程序,并尝试找到与之对应的系统颜色。
我不知道如何遍历System.Drawing.SystemColors类的所有颜色--它是一个类,而不是枚举或列表。
我如何做到这一点(某种反思?)?
发布于 2012-09-08 16:20:12
怎么样
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-08 16:25:12
然后我做了点东西。
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));
}https://stackoverflow.com/questions/12329138
复制相似问题