Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating enumeration using .NET's CodeDom

Tags:

c#

.net

codedom

I want to create an Enumeration using CodeDom API. I have searched enough on the internet and I get results which are hardly of any use.

What I want to generate is

public enum bug_tracker_type
{
    [Description("Bugzilla")]
    Bugzilla,
    [Description("Debbugs")]
    Debbugs,
    [Description("PHP Project Bugtracker")]
    PHP_Project_Bugtracker,
    [Description("Google Code")]
    Google_Code
}

I used CodeTypeDeclaration and set it's IsEnum property as true, created a name, and set it's Attributes.

Now the biggest problem is how to populate the body?

I tried

CodeTypeMember mem = new CodeTypeMember();
mem.Name = WadlSharpUtils.CreateIdentifier(discreteValue.value);
mem.CustomAttributes.Add(new CodeAttributeDeclaration(discreteValue.value));
// enumCandidate is an instance of CodeTypeDeclaration
enumCandidate.Members.Add(mem);

Though using this solution I can generate the Description attributes, the end of line would be ; and not ,

like image 307
Manish Sinha Avatar asked Apr 17 '10 07:04

Manish Sinha


People also ask

How do I create an enum in Visual Studio?

After all, enum is also a type! And often you would want to create one file for one enum definition. For doing so, you have to create a blank code file and add the namespace definition in it. After that, you have to insert the enum snippet in the file.

Can enum have methods C#?

C# Does not allow use of methods in enumerators as it is not a class based principle, but rather an 2 dimensional array with a string and value.

How does enum work in C#?

In the C# language, enum (also called enumeration) is a user-defined value type used to represent a list of named integer constants. It is created using the enum keyword inside a class, structure, or namespace. It improves a program's readability, maintainability and reduces complexity.

What is enum in asp net core?

An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. To define an enumeration type, use the enum keyword and specify the names of enum members: C# Copy.


1 Answers

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 member name. In your real code, of course, these can be different.)

A 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,                         // trailing comma
}

This is permitted by the C# language, precisely in order to support generated-code scenarios like this, and will compile fine even if it looks a bit odd to the human reader.

like image 136
itowlson Avatar answered Oct 05 '22 23:10

itowlson