Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

c# - Iterating over class properties

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.

Does anyone know if its possible to iterate over a class' properties be it static or object based?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...