Enum members are fields, so use CodeMemberField:
CodeTypeDeclaration type = new CodeTypeDeclaration("BugTracker"); type.IsEnum = true; foreach (var valueName in new string[] { "Bugzilla", "Redmine" }) { // Creates the enum member CodeMemberField f = new CodeMemberField("BugTracker", valueName); // Adds the description attribute f.CustomAttributes.Add(new CodeAttributeDeclaration("Description", new CodeAttributeArgument(new CodePrimitiveExpression(valueName)))); type.Members.Add(f); }
(In this simplified code, the description will always be the same as the name of the participant. In your real code, of course, they can be different.)
The little quirk you may notice is that CodeDom adds a comma after the last enum value:
public enum BugTracker { [Description("Bugzilla")] Bugzilla, [Description("Redmine")] Redmine,
This is allowed in C # to support scripts of generated code like this, and will compile fine even if it looks a little strange to the reader.
itowlson
source share