Creating DescriptionAttribute on Enumeration Field using System.Reflection.Emit

Posted by Manish Sinha on Stack Overflow See other posts from Stack Overflow or by Manish Sinha
Published on 2010-05-22T15:46:05Z Indexed on 2010/05/22 15:50 UTC
Read the original article Hit count: 437

Filed under:
|
|
|
|

I have a list of strings which are candidates for Enumerations values. They are

  • Don't send diffs
  • 500 lines
  • 1000 lines
  • 5000 lines
  • Send entire diff

The problem is that spaces, special characters are not a part of identifiers and even cannot start with a number, so I would be sanitizing these values to only chars, numbers and _

To keep the original values I thought of putting these strings in the DescriptionAttribute, such that the final Enum should look like

public enum DiffBehvaiour
{ 
    [Description("Don't send diffs")]
    Dont_send_diffs,
    [Description("500 lines")]
    Diff_500_lines,
    [Description("1000 lines")]
    Diff_1000_lines,
    [Description("5000 lines")]
    Diff_5000_lines,
    [Description("Send entire diff")]
    Send_entire_diff
}

Then later using code I will retrieve the real string associated with the enumeration value, so that the correct string can be sent back the web service to get the correct resource.

I want to know how to create the DescriptionAttribute using System.Reflection.Emit

Basically the question is where and how to store the original string so that when the Enumeration value is chosen, the corresponding value can be retrieved.

I am also interested in knowing how to access DescriptionAttribute when needed.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET