Reputation: 165
I have WCF service with Enum as data contract. How to get list of Enum values. This is code:
in WCF
[DataContract]
public enum MyEnum
{
[EnumMember(Value="My first member")]
First,
[EnumMember(Value="My second member")]
Second,
[EnumMember(Value="My third member")]
Third
}
in client application:
Array myEnumMembers = Enum.GetValues(typeof(MyEnum));
foreach(MyEnum member in myEnumMembers )
{
MembersListBoxControl.Items.Add(member.ToString());
}
This works but in my list box control it shows value without space like this:
Myfirstmember
Mysecondmember
Mythirdmember
Upvotes: 2
Views: 3204
Reputation: 28345
As the values of Enum
must follow the same naming rules as all identifiers in C#, I think this can't be done like this.
You can either use some Resources file to store string representation or try to use a Description
attribute, like this:
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.
Upvotes: 1