To get the description attribute of an enum value in C#, you can use reflection to access the attribute and retrieve its value. Here's an example code block:
main.cs757 chars31 lines
In this example, we have an enum MyEnum
with three values that have associated description attributes. We retrieve the description attribute value for a specific enum value option
using reflection. The GetField
method is used to get a FieldInfo
object for the enum value, and then the GetCustomAttribute<DescriptionAttribute>
method is used to retrieve the DescriptionAttribute
object associated with that field. From the attribute object, we can then get the Description
property value.
gistlibby LogSnag