如何从 Enum 中获取成员名称列表?

发布于 2024-09-13 08:49:14 字数 575 浏览 2 评论 0原文

这应该是一个相当简单的问题。我正在使用 DocX 库创建新的 Word 文档。我想制作一个测试Word文档,看看每个TableDesign(枚举)是什么样子,以选择我需要的一个。

可应用于表格的设计\样式。 命名空间:Novacode 程序集:DocX(在 DocX.dll 中)版本:1.0.0.10 (1.0.0.10)

语法:

公共枚举TableDesign

会员姓名
定制
表法线
表格网格
光照
LightShadingAccent1
....

等等。我想获得这些 TableDesign 的列表,以便我可以在使用新设计创建新表的方法中重用它以实现所有可能性,但我真的不知道如何从该枚举中获取列表:

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

我如何获得它?

This should be fairly simple question. I'm using DocX library to create new word documents. I wanted to make a test word document to see how each TableDesign (enum) looks like to choose the one I need.

Designs\Styles that can be applied to a table.
Namespace: Novacode
Assembly: DocX (in DocX.dll) Version: 1.0.0.10 (1.0.0.10)

Syntax:

public enum TableDesign

Member name
Custom
TableNormal
TableGrid
LightShading
LightShadingAccent1
....

And so on. I would like to get a list of those TableDesign's so i could reuse it in a method creating new table with new design for all possibilities, but I don't really know how to get the list from that enum:

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

How do I get that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演多会厌 2024-09-20 08:49:14

我自己找到了答案:

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

对于我使用的具体情况:

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

并且在 testMethod 中我:

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

它工作没有问题(即使很慢,但我只是想快速得到东西(并且一次性性能并不重要)也许

它也会对将来的人有所帮助:-)

Found answer myself:

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

For my specific case I've used:

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

and in testMethod I've:

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

It worked without a problem (even if it was slow, but I just wanted to get things quickly (and being onetimer performance didn't matter).

Maybe it will help someone in future too :-)

划一舟意中人 2024-09-20 08:49:14

或者:

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

GetValue 将以 enum 的形式返回值的 Volume[]。打印 enum 值将调用其 ToString(),并按其名称进行渲染。转换为 int (优于 byte)将给出其编号。

Alternately:

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

GetValue will return an Volume[] of the values as enums. Printing an enum value will call its ToString(), rendering it by it name. Casting to int (better than byte) will give its number.

甜尕妞 2024-09-20 08:49:14

我想对 MadBoy 的答案添加评论,不知道为什么我不能...
无论如何,就像他说的那样,这就是要走的路,

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

我也认为测试可能看起来

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

更自然
最后,关于性能问题,我认为枚举往往很小,所以我根本不用担心

I wanted to add a comment on MadBoy's answer, dont know why i cant...
anyway like he said thats the way to go

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

also i think the testing could be like

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

it just seems more natural
last, about the performance issue, i think enums tend to be very small so i wont be worried at all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文