通過重寫 class 的 ToString() 來簡化獲取 enum 的 DescriptionAttribute 值

来源:https://www.cnblogs.com/liqingwen/archive/2018/02/06/8415440.html
-Advertisement-
Play Games

通過重寫 class 的 ToString() 來簡化獲取 enum 的 DescriptionAttribute 值 目錄 一、常見的 enum 類型 二、演變:class 版本的 enum 類型 三、演進:class 和 enum 兩者共存的版本 一、常見的 enum 類型 新建一個 Algor ...


通過重寫 class 的 ToString() 來簡化獲取 enum 的 DescriptionAttribute 值

 

目錄

 

一、常見的 enum 類型

  新建一個 AlgorithmType 枚舉,裡面包含 MD5、SHA1 和 SHA2 等一系列的枚舉值,預設為 int 類型。有的時候我們會在對應的枚舉值上使用特性 [Description] 來添加備註信息,方便我們後期提供描述信息(如返回給前端界面展示時可能需要使用)。

  AlgorithmType.cs

    /// <summary>
    /// 演算法類型
    /// </summary>
    public enum AlgorithmType
    {
        /// <summary>
        /// MD5
        /// </summary>
        [Description("Message-Digest Algorithm 5")]
        MD5,

        /// <summary>
        /// SHA1
        /// </summary>
        [Description("Secure Hash Algorithm 1")]
        SHA1,

        /// <summary>
        /// SHA224
        /// </summary>
        [Description("Secure Hash Algorithm 224")]
        SHA224,

        /// <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
    }

 

  常見的一個做法是,編寫一個擴展方法來獲取 enum 的描述信息:

    public static class EnumExtensionMethods
    {
        /// <summary>
        /// 獲取枚舉類型的描述信息
        /// </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.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute))
            {
                return name;
            }

            return attribute.Description;
        }
    }

  

  因為直接輸出枚舉值,或者通過枚舉類型對應的 ToString() 的方式輸出字元串,結果都為它本身的文本值,BCL 也並沒有提供快捷的方式去獲取描述信息,所以只能通過類似上述的擴展方法來獲取 [DescriptionAttribute]。

  

  【分析】因為枚舉屬於值類型,它不需要在堆上分配空間,並且在計算時(如比較運算)效率相比引用類型更高,所以從性能上枚舉類型是占據了絕對的優勢;但是由於 BCL 並沒有提供快捷方式的去獲取 [Description],並且編寫對應的獲取方法也較為麻煩(其實也不麻煩,因為你會使用程式員的兩大法寶:CTRL+C 和 CTRL+V)。

 

二、演變:class 版本的 enum 類型 

  這次我們不直接使用 enum,而是採取新建 class 的方式去模仿 enum 類型,也就是使用引用類型來取代值類型,不過,性能方面會有所損耗。

  HashAlgorithmType.cs

    /// <summary>
    /// 哈希演算法類型
    /// </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("哈希演算法類型有誤");
            }

            return result;
        }
    }

  我們採用了 private 進行構造函數私有化的操作,這樣就不會允許在類的外部 new 對象了;其次,使用 public static readonly 欄位提供給外部進行訪問,這樣的話就和枚舉類型的調用方式一致;最後,我們對 ToString() 方法進行了重寫,在 return 的值中返回對應的描述信息。

 

  這樣,我們就可以直接通過 class.field 的方式得到對應枚舉值的描述信息。

 

  【分析】性能受損;但 ToString() 比 GetDescription() 更淺顯直白、清晰明瞭;不過,參數以數字“寫死”的方式進行提供也欠妥。

 

三、演進:class 和 enum 兩者共存的版本

  在上一個版本的類中,我們在進行構造函數初始化時直接使用了數字 0~5,並且重寫 ToString() 時也是直接使用數字 0~5,除了不直觀的因素之外,隨著枚舉值數量的增加,枚舉值和自身描述兩者間的對應關係也容易出錯。現在,我們嘗試以私有嵌套類的形式將 enum 和 class 的兩者有機結合起來。

  HashAlgorithmType.cs

    /// <summary>
    /// 哈希演算法類型
    /// </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>
        /// SHA224
        /// </summary>
        public static readonly HashAlgorithmType SHA224 = new HashAlgorithmType(AlgorithmType.SHA224);

        /// <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>
        /// 演算法類型
        /// </summary>
        private readonly AlgorithmType _algorithmType;

        private HashAlgorithmType(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.SHA224:
                    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("哈希演算法類型有誤");
            }

            return result;
        }

        /// <summary>
        /// 演算法類型
        /// </summary>
        private enum AlgorithmType
        {
            /// <summary>
            /// MD5
            /// </summary>
            MD5,

            /// <summary>
            /// SHA1
            /// </summary>
            SHA1,

            /// <summary>
            /// SHA224
            /// </summary>
            SHA224,

            /// <summary>
            /// SHA256
            /// </summary>
            SHA256,

            /// <summary>
            /// SHA384
            /// </summary>
            SHA384,

            /// <summary>
            /// SHA512
            /// </summary>
            SHA512
        }
    }

  在 HashAlgorithmType 類中,新建了一個私有的 AlgorithmType 枚舉,這樣就只允許 HashAlgorithmType 類訪問,而不會在該類的外部(其它類型)進行訪問。

 

  正所謂蘿蔔青菜,各有所愛,假如你過於關於效率性能,或者說不需要使用諸如 [Description] 附加的特性,也不想額外的增添更多的行為和特征時,我們依然可以採用最原始的、大眾化的版本。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • "回到目錄" Aspect面向方面編程 面向側面的程式設計(aspect oriented programming,AOP,又譯作面向方面的程式設計、觀點導向編程、剖面導向程式設計)是電腦科學中的一個術語,指一種程式設計範型。該範型以一種稱為側面(aspect,又譯作方面)的語言構造為基礎,側面是 ...
  • 一、自定義控制項的基本步驟: (本示例項目名稱為:W;添加的自定義控制項名稱為) 1、 在“解決方案資源管理器”視窗的項目名上: 右擊à添加à新建項(Ctrl+Shift+A) 2、則會彈出如下視窗,在該視窗中選擇“自定義控制項(WPF)”並修改類“名稱”,點擊“添加” 3、添加成功後則會在該項目中生成C ...
  • 學習.NET的正則表達式時,對零寬斷言比較迷惑,拿出時間學習了一下,做個筆記。 零寬斷言概述 (?<=pattern) (?<!pattern) STRING (?=pattern) (?!pattern) :各種斷言出現的相對位置 ?< lookbehind STRING <lookahead : ...
  • 這是MVVM之旅系列文章的第一篇,許多文章和書喜歡在開篇介紹某種技術的誕生背景和意義,但是我覺得對於程式員來說,一個能直接運行起來的程式或許能夠更直觀的讓他們瞭解這種技術。在這篇文章里,我將帶領大家一步一步創建一個最簡單的MVVM程式,程式雖然簡單,但是卻涵蓋了MVVM的基本要素,對於那些還不是很了 ...
  • 當一份web報表項目壓縮包躺在我的文件夾里時,我是完全懵的。作為一個學習了一個月java的asp.net小白,以前從來沒有接觸過這方面,我完全不知道從何入手。(出於其他原因,不方便貼圖,貼代碼) 手裡也有asp.net開發學習視頻,但都因為懶沒看。網上搜集了很多資料,得知這種web報表一般是由三層物 ...
  • 前言 童鞋們,大家好 我是專註.NET開發者社區建設的實踐者Rector。 首先,為自己間隔了兩個星期五再更新本系列文章找個不充分的理由:Rector最近工作,家庭的各種事務所致,希望大家諒解。 本文知識要點 回到本文的主題,還是關於系列文章:《一步一步創建ASP.NET MVC5程式Reposit ...
  • 前言:前面準備了那麼久的準備工作,現在終於可以開始構建我們自己的服務了。這篇博客就讓我們一起構建自己的第一個服務 審計日誌。 首先我們先創建兩個項目,一個控制台的服務啟動項目,一個業務的實現項目。(註:控制台項目可以引用業務項目,也可以不引用業務項目。因為surging支持熱部署) 在服務啟動項目中 ...
  • 1. 前言 HeaderedContentControl是WPF中就存在的控制項,這個控制項的功能很簡單:提供Header和Content兩個屬性,在UI上創建兩個ContentPresenter並分別綁定到Header和Content,讓這兩個ContentPresenter合體組成HeaderedC ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...