C # traverse struct
Type type = typeof(Color); PropertyInfo[] fields = type.GetProperties(); //FieldInfo[] fields =type.GetFields(System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.Static); int i = 30; panel1.Controls.Clear(); foreach (PropertyInfo f in fields) { if (f.ToString().IndexOf(".Color ") > 0) { Button btn = new Button(); btn.BackColor = Color.FromName(f.Name); btn.Text = f.Name; btn.Location = new Point((i % 10) * 125, 10 + (i / 10) * 30); //btn.AutoSize = true; btn.Width = 120; i++; panel1.Controls.Add(btn); } }
C # traversal Enumeration
The enum class can be used for getnames and getvalues. For example, the windowstate enumeration foreach (string s in Enum. getnames (this. windowstate. getType () label1.text + = (S + "\ r \ n"); foreach (int I in Enum. getvalues (this. windowstate. getType () label1.text + = (I. tostring () + "\ r \ n ");
Sometimes the enumerated value cannot be obtained through getvalues. Because the enumerated values are of the int type, you can obtain the following values:
(INT) enum. getnames (this. windowstate. GetType () [0];