2

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
user2880783
  • 145
  • 6
  • 18

1 Answers1

1

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.

MSDN article about attribute

Community
  • 1
  • 1
VMAtm
  • 27,943
  • 17
  • 79
  • 125
  • Not sure that this helps. I have to select enum form list so I can send it back to wcf. I can't do that whit Decription property. – user2880783 Aug 07 '14 at 12:27
  • @user2880783 You should add description to the TExt, and enum Value to the Value of the `ListItem`. – VMAtm Aug 08 '14 at 09:06