How is Enum used?

Source: Internet
Author: User

Everyone is familiar with Enum enumeration. However, when enumeration is used in many scenarios, can we abstract a common solution. Everyone writes the code, but not everyone likes to write repeated code. Do you always use Ctrl + C or Ctrl + V to get tired? Like me, many people do not like to write repeated code. If code is too much to write, there are more bugs. For common scenarios, most people like to abstract them and write a set of common ones, which can be used everywhere and are not prone to errors. Of course, you like Ctrl + C and Ctrl + V, and I can't do it myself ....

A simple enumeration is as follows:

     , = = 

Enter the following code:

            ExchangeType type = (ExchangeType) (type ==  (type ==

For forced conversion, Let's guess what will be output ....

You can test the results on your own, which may be different from the expected value!

The following code also applies:

 success = Enum.TryParse<ExchangeType>(,   type);

The return result success of TryParse execution is successful (true), but the expected value is not necessarily correct.

Therefore, enumeration conversion is prone to errors. Therefore, the default value must be provided to ensure that the correct value is returned when the conversion fails. The code for converting int and string values to Enum is as follows:

          T ToEnum<T>( value, T defaultT)  T :  enumName = Enum.GetName( ToEnum<T>  T ToEnum<T>( enumName, T defaultT)  T :  ( (!Enum.TryParse<T>(enumName.Trim(),  (Enum.IsDefined(


In addition, other methods are provided. For example, to convert the string ExchangeType. All containing the type name prefix to an enumeration, you also need to provide the default value to ensure the accuracy of the conversion result. The Code is as follows:

          T TryParse<T>( typeValue, T defaultValue,  containsTypeName = )  T : = (typeValue ??  startIndex = typeValue.IndexOf( (startIndex >  && typeValue.Length > startIndex + = typeValue.Substring(startIndex +  ToEnum<T>

The application is relatively simple. ExchangeType type = EnumFieldProvider. TryParse <ExchangeType> ("ExchangeType.", ExchangeType. All, true );

In the drop-down list, many scenarios are related to enumeration, and hard encoding can also be bound directly. But do I have to write a lot of code when enumerating a lot, and it does not necessarily ensure the correctness of the Code. In this case, let's write a general application. Everyone can easily:

          (type ==  [] customAttributes = type.GetCustomAttributes((EnumFieldAttribute), =  ( attr = attr  (attribute !=    IList<EnumItem<T>> GetItems<T>( isSpecialRequired = )  T :  fields = (T).GetFields(BindingFlags.Static | (fields ==  || fields.Count() ==   List<EnumItem<T>> enumItems =  List<EnumItem<T>>=  ( field [] customAttributes = field.GetCustomAttributes((EnumFieldAttribute),  ( attr = attr  (attribute !=  (!= (T)field.GetValue( EnumItem<T>  IList<EnumItem<T>> GetItems<T>(IList<T> entities)  T :  (entities ==  || entities.Count() ==   List<EnumItem<T>> enumItems =  List<EnumItem<T>> ( entity <T>   GetItemDescription<T>(T enumItem)  T :  field =  (field ==  [] customAttributes = field.GetCustomAttributes((EnumFieldAttribute),  ( attr = attr  (attribute !=    EnumItem<T> GetItem<T>(T enumItem)  T :  field =  (field ==   EnumItem<T>[] customAttributes = field.GetCustomAttributes((EnumFieldAttribute),  ( attr = attr  (attribute !=   EnumItem<T>  EnumItem<T>

For the public static IList <EnumItem <T> GetItems <T> (bool isSpecialRequired = false) where T: struct method, a parameter is provided to filter whether this parameter is required, for example, in the EnumFieldAttribute ("unlimited", true) of the enumerated type ExchangeType defined above, the second parameter EnumFieldAttribute ("unlimited", true) is consistent with the isSpecialRequired parameter.
The test code is as follows:

 <EnumItem<ExchangeType>> actual = EnumFieldProvider.GetItems<ExchangeType>( <EnumItem<ExchangeType>> actual = EnumFieldProvider.GetItems<ExchangeType>

The returned results can be used to bind and display the values of related controls such as the drop-down list and ListBox. (This article does not provide the results because you have to go to work tomorrow. It will be provided in the next article ).

It takes time and effort to summarize the details. If you use it more, you will naturally understand it. You have washed and slept...

Today is July 1, and it is another anniversary. Four years ago, after the graduation gala in June 26, the first day of his formal work was also in July 1. In a twinkling of an eye, four years have passed, and life is like a play...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.