Iterating over class properties

前端 未结 2 1487
囚心锁ツ
囚心锁ツ 2020-12-16 13:16

I\'m trying to iterate over the Color class\' Color properties.

Unfortunately its not in a collection so its just a class with a bunch of static properties.

2条回答
  •  不思量自难忘°
    2020-12-16 14:11

    Yes, it's possible using reflection. Specific colors are defined as a static properties of the 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
     }
    

提交回复
热议问题