How to get a list of member names from Enum?

This should be a fairly simple question. I use the DocX library to create new text documents. I wanted to make a test word document to see what each TableDesign (enum) looks like, to select the one I need.

Constructions \ Styles that can be applied to the table. Namespace: Novacode Build: DocX (in DocX.dll) Version: 1.0.0.10 (1.0.0.10)

Syntax:

public enum TableDesign

Username
 Custom
 TableNormal
 TableGrid
 LightShading
 LightShadingAccent1
 ....

Etc. I would like to get a list of these TableDesign so that I can reuse it in a method that creates a new table with a new design for all the features, but I really don't know how to get the list from this listing:

foreach (var test in TableDesign) {
      createTable(documentWord, test);
}

How do i get this?

+3
source share
3 answers

Found the answer:

    // get a list of member names from Volume enum,
    // figure out the numeric value, and display
    foreach (string volume in Enum.GetNames(typeof(Volume)))
    {
        Console.WriteLine("Volume Member: {0}\n Value: {1}",
            volume, (byte)Enum.Parse(typeof(Volume), volume));
    }

In my specific case, I used:

 foreach (var test in Enum.GetNames(typeof(TableDesign))) {
     testMethod(documentWord, test);
 }

and in the testMethod method:

tableTest.Design = (TableDesign) Enum.Parse(typeof(TableDesign), test); 

It worked without problems (even if it was slow, but I just wanted to get things fast (and the performance of onetimer doesn't matter).

Perhaps this will help someone in the future :-)

+12
source

As an alternative:

foreach (var volume in Enum.GetValues(typeof(Volume))) 
{ 
    Console.WriteLine("Volume Member: {0}\n Value: {1}", 
        volume, (int) volume); 
} 

GetValue Volume[] enum s. enum ToString(), . int (, byte) .

+3

I wanted to add a comment to MadBoy’s request, I don’t know why I can’t ...
anyway, as he said, this is the way


foreach (TableDesign t in Enum.GetNames(typeof(TableDesign)))
{
     // do work
}

I also think testing might be similar to


bool defined = Enum.IsDefined(typeof(TableDesign), value);

it just seems more natural the
last one, about the performance issue, I think the listings tend to be very small, so I won’t worry at all

0
source

Source: https://habr.com/ru/post/1758773/


All Articles