17
Q
迭代类属性
A
回答
29
是的,它可以使用反射。特定颜色被定义为Color struct
的静态属性。
PropertyInfo[] colors = typeof(Color).GetProperties(BindingFlags.Static|BindingFlags.Public);
foreach(PropertyInfo pi in colors) {
Color c = (Color)pi.GetValue(null, null);
// do something here with the color
}
2
您可能也有兴趣在此代码
http://blog.guymahieu.com/2006/07/11/deep-reflection-of-properties-propertyreflector/
它提供了一种简单的方法来设置/按名称获取属性。如果你看看GetBestMatchingProperty你会发现在性能迭代,做了同样的方式之前 Iterating over class properties
被张贴我想补充: 如果(pi.PropertyType == typeof运算(彩色)) 到面向未来的任何新特性都将添加到Color中。 – ICR 2009-02-21 13:19:35