Traversal enumerated type experience

1. Get enumerated field with the reflection, the first field is a field with the system, type Int, Name seemingly _value.

Need to filter out;

fi.FieldType == typeof(T);

or

 if (fi.IsSpecialName) continue;

 

In addition Gets Name, Value, Description

                string name = fi.Name;
                object value = Convert.ToInt32(fi.GetRawConstantValue());
                DescriptionAttribute[] descs = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                var desc = descs.Length > 0 ? descs[0].Description : "";

                Console.WriteLine(string.Format("name:{0}; value:{1} ; des:{2}", name, value, desc));

 

Further, Enum There is a static method, GetValues ​​() returns the object [] Inside be name, or strong int turn can obtain the corresponding value.

 

 

 

Reproduced in: https: //www.cnblogs.com/xinjian/p/3595406.html

Guess you like

Origin blog.csdn.net/weixin_34081595/article/details/93822268