Override the ToString () of the class to simplify obtaining the DescriptionAttribute value of enum,

Source: Internet
Author: User

Override the ToString () of the class to simplify obtaining the DescriptionAttribute value of enum,
Override the ToString () of the class to simplify obtaining the DescriptionAttribute value of enum.

 

Directory
  • I. Common enum types
  • Ii. Evolution: enum type of the class version
  • Iii. Evolution: versions of both class and enum

 

I. Common enum types

Create an AlgorithmType enumeration that contains MD5, SHA1, SHA2, and other enumerated values. The default value is int. Sometimes we will use the [Description] feature on the corresponding enumerated values to add the remarks, so that we can provide the Description information later (for example, we may need to use it when returning it to the front-end interface for display ).

AlgorithmType. cs

/// <Summary> /// algorithm type /// </summary> public enum AlgorithmType {/// <summary> /// MD5 /// </summary> [description ("Message-Digest Algorithm 5")] MD5, /// <summary> /// SHA1 /// </summary> [Description ("Secure Hash Algorithm 1")] SHA1, /// <summary> /// shares' // </summary> [Description ("Secure Hash Algorithm 224")] shares, /// <summary> /// SHA256 /// </summary> [Description ("Secure Hash Algorithm 256")] SHA256, /// <summary> /// SHA384 /// </summary> [Description ("Secure Hash Algorithm 384")] SHA384, /// <summary> /// SHA512 /// </summary> [Description ("Secure Hash Algorithm 512")] SHA512}

 

A common practice is to compile an extension method to obtain the enum description:

Public static class EnumExtensionMethods {// <summary> // obtain the description of the enumeration type /// </summary> public static string GetDescription (this Enum value) {var type = value. getType (); var name = Enum. getName (type, value); if (name = null) return null; var field = type. getField (name); if (! (Attribute. getcustomattriof (field, typeof (DescriptionAttribute) is DescriptionAttribute) {return name;} return attribute. Description ;}}

  

Because the enumerated value is output directly, or the string is output by using the ToString () corresponding to the enumerated type, the result is the text value of the string, BCL does not provide a quick way to obtain the description information. Therefore, you can only obtain the [DescriptionAttribute] Using extension methods similar to the preceding.

[Analysis] Because enumeration belongs to the value type, it does not need to allocate space on the stack, and is more efficient than the reference type in computing (such as comparison calculation, therefore, enumeration type has an absolute advantage in terms of performance. However, because BCL does not provide a shortcut to obtain [Description], writing the corresponding retrieval method is also more troublesome (in fact, it is not troublesome, because you will use the two magic weapons of programmers: CTRL + C and CTRL + V ).

 

Ii. Evolution: enum type of the class version

This time, we do not directly use enum. Instead, we use the new class method to simulate the enum type, that is, to replace the value type with the reference type. However, performance will be compromised.

HashAlgorithmType. cs

/// <Summary> /// hash algorithm type /// </summary> public class HashAlgorithmType {// <summary> /// MD5 /// </summary> public static readonly HashAlgorithmType MD5 = new HashAlgorithmType (0 ); /// <summary> /// SHA1 /// </summary> public static readonly HashAlgorithmType SHA1 = new HashAlgorithmType (1 ); /// <summary> /// SHA224 /// </summary> public static readonly HashAlgorithmType SHA224 = new HashAlgorithmType (2 ); /// <summary> /// SHA256 /// </summary> public static readonly HashAlgorithmType SHA256 = new HashAlgorithmType (3 ); /// <summary> /// SHA384 /// </summary> public static readonly HashAlgorithmType SHA384 = new HashAlgorithmType (4 ); /// <summary> /// SHA512 /// </summary> public static readonly HashAlgorithmType SHA512 = new HashAlgorithmType (5); private readonly int _ hashAlgorithmType; private HashAlgorithmType (int hashAlgorithmType) {_ hashAlgorithmType = hashAlgorithmType;} public override string ToString () {string result; switch (_ hashAlgorithmType) {case 0: result = "Message-Digest Algorithm 5"; break; case 1: result = "Secure Hash Algorithm 1"; break; case 2: result = "Secure Hash Algorithm 224"; break; case 3: result = "Secure Hash Algorithm 256"; break; case 4: result = "Secure Hash Algorithm 384"; break; case 5: result = "Secure Hash Algorithm 512 "; break; default: throw new Exception ("Incorrect hash algorithm type");} return result ;}}

We adopt private to privatize the constructor, so that the new object outside the class is not allowed. Secondly, the public static readonly field is used for external access, in this way, it is consistent with the calling method of the enumeration type. Finally, we overwrite the ToString () method and return the corresponding description information in the return value.

 

[Analysis] The performance is impaired. However, ToString () is more straightforward and clear than GetDescription (). However, the parameter is not provided by the number "writing to death.

 

Iii. Evolution: versions of both class and enum

In the class of the previous version, the number 0 ~ is used directly during the initialization of the constructor ~ 5, and the number 0 ~ is used directly when the ToString () is rewritten ~ 5. Apart from the inintuitive factors, as the number of enumerated values increases, the correspondence between the enumerated values and their own descriptions is also prone to errors. Now, we try to combine enum and class in the form of private Nested classes.

HashAlgorithmType. cs

/// <Summary> /// hash algorithm type /// </summary> public class HashAlgorithmType {// <summary> /// MD5 /// </summary> public static HashAlgorithmType MD5 = new HashAlgorithmType (AlgorithmType. MD5); // <summary> // SHA1 // </summary> public static readonly HashAlgorithmType SHA1 = new HashAlgorithmType (AlgorithmType. SHA1); // <summary> /// shahoo/// </summary> public static readonly HashAlgorithmType SHA224 = new HashAlgorithmType (AlgorithmType. shares'); // <summary> /// SHA256 // </summary> public static readonly HashAlgorithmType SHA256 = new HashAlgorithmType (AlgorithmType. SHA256); // <summary> // SHA384 // </summary> public static readonly HashAlgorithmType SHA384 = new HashAlgorithmType (AlgorithmType. SHA384); // <summary> /// SHA512 // </summary> public static readonly HashAlgorithmType SHA512 = new HashAlgorithmType (AlgorithmType. SHA512); // <summary> // algorithm type // </summary> private readonly AlgorithmType _ algorithmType; private partition (AlgorithmType algorithmType) {_ algorithmType = algorithmType ;} public override string ToString () {string result; switch (_ algorithmType) {case AlgorithmType. MD5: result = "Message-Digest Algorithm 5"; break; case AlgorithmType. SHA1: result = "Secure Hash Algorithm 1"; break; case AlgorithmType. shavel: result = "Secure Hash Algorithm 224"; break; case AlgorithmType. SHA256: result = "Secure Hash Algorithm 256"; break; case AlgorithmType. SHA384: result = "Secure Hash Algorithm 384"; break; case AlgorithmType. SHA512: result = "Secure Hash Algorithm 512"; break; default: throw new Exception ("Incorrect Hash Algorithm type");} return result ;} /// <summary> /// algorithm type /// </summary> private enum AlgorithmType {// <summary> /// MD5 // </summary>, /// <summary> /// SHA1 /// </summary> SHA1, /// <summary> /// sha1_// </summary> sha1, /// <summary> /// SHA256 /// </summary> SHA256, /// <summary> /// SHA384 /// </summary> SHA384, /// <summary> /// SHA512 /// </summary> SHA512 }}

In the HashAlgorithmType class, a private AlgorithmType enumeration is created. In this way, only access to the HashAlgorithmType class is allowed, instead of outside (other types) of the class.

 

The so-called radish and vegetables have their own love. If you are too concerned about efficiency and performance, or do not need to use additional features such as [Description] or add more behaviors and features, we can still use the most primitive and popular version.

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.