Tag Archives: enum description
C# || How To Set & Get The Description Of An Enum Using C#
The following is a module with functions which demonstrates how to set and get the description of an enum using C#.
This function uses the DescriptionAttribute to get the description of an Enum element. If a description attribute is not specified, the string value of the Enum element is returned.
1. Get Description
The example below demonstrates the use of ‘Utils.Extensions.GetDescription‘ to get the description of enum elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// Get Description using Utils; public enum MimeTypes { [System.ComponentModel.Description("application/pdf")] ApplicationPDF = 1, [System.ComponentModel.Description("application/octet-stream")] ApplicationOctect = 2, [System.ComponentModel.Description("application/zip")] ApplicationZip = 3, [System.ComponentModel.Description("application/msword")] ApplicationMSWordDOC = 4, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.wordprocessingml.document")] ApplicationMSWordDOCX = 5, [System.ComponentModel.Description("application/vnd.ms-excel")] ApplicationMSExcelXLS = 6, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")] ApplicationMSExcelXLSX = 7 } foreach (MimeTypes value in System.Enum.GetValues(typeof(MimeTypes))) { Console.WriteLine($"Numeric Value: {(int)value}, Name: {value.ToString()}, Description: {value.GetDescription()}"); } // expected output: /* Numeric Value: 1, Name: ApplicationPDF, Description: application/pdf Numeric Value: 2, Name: ApplicationOctect, Description: application/octet-stream Numeric Value: 3, Name: ApplicationZip, Description: application/zip Numeric Value: 4, Name: ApplicationMSWordDOC, Description: application/msword Numeric Value: 5, Name: ApplicationMSWordDOCX, Description: application/vnd.openxmlformats-officedocument.wordprocessingml.document Numeric Value: 6, Name: ApplicationMSExcelXLS, Description: application/vnd.ms-excel Numeric Value: 7, Name: ApplicationMSExcelXLSX, Description: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 9, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; using System.Linq; namespace Utils { public static class Extensions { /// <summary> /// Returns the description of an enum decorated with a DescriptionAttribute /// </summary> /// <param name="value">An Enumeration value</param> /// <returns>The description of an enumeration value</returns> public static string GetDescription(this System.Enum value) { var description = value.ToString(); var attribute = GetAttribute<System.ComponentModel.DescriptionAttribute>(value); if (attribute != null) { description = attribute.Description; } return description; } public static T GetAttribute<T>(System.Enum value) where T : System.Attribute { var field = value.GetType().GetField(value.ToString()); var attribute = ((T[])field.GetCustomAttributes(typeof(T), false)).FirstOrDefault(); return attribute; } } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 9, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using Utils; public class Program { public enum MimeTypes { [System.ComponentModel.Description("application/pdf")] ApplicationPDF = 1, [System.ComponentModel.Description("application/octet-stream")] ApplicationOctect = 2, [System.ComponentModel.Description("application/zip")] ApplicationZip = 3, [System.ComponentModel.Description("application/msword")] ApplicationMSWordDOC = 4, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.wordprocessingml.document")] ApplicationMSWordDOCX = 5, [System.ComponentModel.Description("application/vnd.ms-excel")] ApplicationMSExcelXLS = 6, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")] ApplicationMSExcelXLSX = 7, [System.ComponentModel.Description("application/vnd.ms-powerpoint")] ApplicationMSPowerPointPPT = 8, [System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.presentationml.presentation")] ApplicationMSPowerPointPPTX = 9, [System.ComponentModel.Description("audio/basic")] AudioBasic = 10, [System.ComponentModel.Description("audio/mpeg")] AudioMPEG = 11, [System.ComponentModel.Description("audio/x-wav")] AudioWAV = 12, [System.ComponentModel.Description("image/gif")] ImageGIF = 13, [System.ComponentModel.Description("image/jpeg")] ImageJPEG = 14, [System.ComponentModel.Description("image/png")] ImagePNG = 15, [System.ComponentModel.Description("text/html")] TextHTML = 16, [System.ComponentModel.Description("text/plain")] TextPlain = 17, [System.ComponentModel.Description("video/mpeg")] VideoMPEG = 18, [System.ComponentModel.Description("video/x-msvideo")] VideoAVI = 19 } static void Main(string[] args) { try { foreach (MimeTypes value in System.Enum.GetValues(typeof(MimeTypes))) { Display($"Numeric Value: {(int)value}, Name: {value.ToString()}, Description: {value.GetDescription()}"); } } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || How To Set & Get The Description Of An Enum Using VB.NET
The following is a module with functions which demonstrates how to set and get the description of an enum using VB.NET.
This function uses the DescriptionAttribute to get the description of an Enum element. If a description attribute is not specified, the string value of the Enum element is returned.
1. Get Description
The example below demonstrates the use of ‘Utils.GetDescription‘ to get the description of enum elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
' Get Description Imports Utils Public Enum MimeTypes <System.ComponentModel.Description("application/pdf")> ApplicationPDF = 1 <System.ComponentModel.Description("application/octet-stream")> ApplicationOctect = 2 <System.ComponentModel.Description("application/zip")> ApplicationZip = 3 <System.ComponentModel.Description("application/msword")> ApplicationMSWordDOC = 4 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.wordprocessingml.document")> ApplicationMSWordDOCX = 5 <System.ComponentModel.Description("application/vnd.ms-excel")> ApplicationMSExcelXLS = 6 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")> ApplicationMSExcelXLSX = 7 End Enum For Each value As MimeTypes In System.[Enum].GetValues(GetType(MimeTypes)) Debug.Print($"Numeric Value: {CInt(value)}, Name: {value.ToString}, Description: {value.GetDescription}") Next ' expected output: ' Numeric Value: 1, Name: ApplicationPDF, Description: application/pdf ' Numeric Value: 2, Name: ApplicationOctect, Description: application/octet-stream ' Numeric Value: 3, Name: ApplicationZip, Description: application/zip ' Numeric Value: 4, Name: ApplicationMSWordDOC, Description: application/msword ' Numeric Value: 5, Name: ApplicationMSWordDOCX, Description: application/vnd.openxmlformats-officedocument.wordprocessingml.document" ' Numeric Value: 6, Name: ApplicationMSExcelXLS, Description: application/vnd.ms-excel ' Numeric Value: 7, Name: ApplicationMSExcelXLSX, Description: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 11, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Returns the description of an enum decorated with a DescriptionAttribute ''' </summary> ''' <param name="value">An Enumeration value</param> ''' <returns>The description of an enumeration value</returns> <Runtime.CompilerServices.Extension()> Public Function GetDescription(value As System.[Enum]) As String Dim description = value.ToString Dim attribute = GetAttribute(Of System.ComponentModel.DescriptionAttribute)(value) If attribute IsNot Nothing Then description = attribute.Description End If Return description End Function Public Function GetAttribute(Of T As {System.Attribute})(value As System.[Enum]) As T Dim field = value.GetType().GetField(value.ToString()) Dim attribute = CType(field.GetCustomAttributes(GetType(T), False), T()).FirstOrDefault Return attribute End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 11, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Imports Utils Module Program Public Enum MimeTypes <System.ComponentModel.Description("application/pdf")> ApplicationPDF = 1 <System.ComponentModel.Description("application/octet-stream")> ApplicationOctect = 2 <System.ComponentModel.Description("application/zip")> ApplicationZip = 3 <System.ComponentModel.Description("application/msword")> ApplicationMSWordDOC = 4 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.wordprocessingml.document")> ApplicationMSWordDOCX = 5 <System.ComponentModel.Description("application/vnd.ms-excel")> ApplicationMSExcelXLS = 6 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")> ApplicationMSExcelXLSX = 7 <System.ComponentModel.Description("application/vnd.ms-powerpoint")> ApplicationMSPowerPointPPT = 8 <System.ComponentModel.Description("application/vnd.openxmlformats-officedocument.presentationml.presentation")> ApplicationMSPowerPointPPTX = 9 <System.ComponentModel.Description("audio/basic")> AudioBasic = 10 <System.ComponentModel.Description("audio/mpeg")> AudioMPEG = 11 <System.ComponentModel.Description("audio/x-wav")> AudioWAV = 12 <System.ComponentModel.Description("image/gif")> ImageGIF = 13 <System.ComponentModel.Description("image/jpeg")> ImageJPEG = 14 <System.ComponentModel.Description("image/png")> ImagePNG = 15 <System.ComponentModel.Description("text/html")> TextHTML = 16 <System.ComponentModel.Description("text/plain")> TextPlain = 17 <System.ComponentModel.Description("video/mpeg")> VideoMPEG = 18 <System.ComponentModel.Description("video/x-msvideo")> VideoAVI = 19 End Enum Sub Main(args As String()) Try For Each value As MimeTypes In System.[Enum].GetValues(GetType(MimeTypes)) Display($"Numeric Value: {CInt(value)}, Name: {value.ToString}, Description: {value.GetDescription}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.