Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose enum attributes to WCF client

Tags:

enums

wcf

I want to expose enum attributes to WCF client application, but I can only see enum values.

Here is the enum:

public enum TemplateType
{
    [EnumDescription("Property Particulars")]
    [EnumValue("PropertyParticulars")]        
    PropertyParticulars = 1,

    [EnumDescription("Short Format Lists")]
    [EnumValue("ShortFormatLists")]        
    ShortFormatLists,

    [EnumDescription("Client Letters")]
    [EnumValue("ClientLetters")]
    ClientLetters,

    [EnumDescription("Labels")]
    [EnumValue("Labels")]
    Labels
}

How can I expose the Description and Value attributes?

like image 979
inutan Avatar asked Jan 07 '10 13:01

inutan


1 Answers

You can expose enums from a service but the attributes on an enum are not serialized when they are sent over the wire. This means that consumers of this enum will only see the enum itself and none of your attributes.

What you need to do is dress up your enum with a DataContract attribute and the values with the EnumMember attribute so that your information will be serialized, but this will only allow you to specify the underlying value of each enum value, not a description.

like image 80
Andrew Hare Avatar answered Sep 24 '22 04:09

Andrew Hare