我有与Enum的WCF服务作为数据合同。如何获取枚举值列表。这是代码:
WCF中的
[DataContract]
public enum MyEnum
{
[EnumMember(Value="My first member")]
First,
[EnumMember(Value="My second member")]
Second,
[EnumMember(Value="My third member")]
Third
}
在客户端应用程序中:
Array myEnumMembers = Enum.GetValues(typeof(MyEnum));
foreach(MyEnum member in myEnumMembers )
{
MembersListBoxControl.Items.Add(member.ToString());
}
这有效,但在我的列表框控件中,它显示没有空格的值,如下所示:
Myfirstmember
Mysecondmember
Mythirdmember
答案 0 :(得分:1)
由于Enum
的值必须follow the same naming rules作为C#中的所有标识符,我认为不能这样做。
您可以使用某些Resources file来存储字符串表示形式,也可以尝试使用Description
attribute,如下所示:
public enum MyEnum
{
[Description("Description for Foo")]
Foo,
[Description("Description for Bar")]
Bar
}
MyEnum x = MyEnum.Foo;
string description = x.GetDescription(); // extension method provided in original answer.