2

I have an enum with a DescriptionAttribute on each member. Since I'm not able to send this DescriptionAttribute over WCF (learned this the hard way), I have been trying to create a method that sends me a dictionary with descriptions. After a bit of struggling around, I've come up with this method in my WCF-service:

public IDictionary<Enum, string> GetEnumDescriptionsList(string enumtype)
{
    Type t = Type.GetType(enumtype);
    if (t.BaseType != typeof(Enum))
    {
        return null;
    }

    var returnvalue = new Dictionary<Enum, string>();
    foreach (Enum value in Enum.GetValues(t))
    {
        returnvalue.Add(value, value.GetDescription());
    }
    return returnvalue;
}

The call to the method:

var myDict = GetEnumDescriptionsList(typeof (UserType).ToString());

This works fine when I make that call in the same class as the method, but when I make the call over WCF I got null returned. After debugging I fnoticed that the string-parameter holds an "incorrect" value. I mean that UserType.ToString() on ther clientside has a different namespace then the one in the WCF-service.

Reading this post explained that issue.

At this moment I'm pretty stuck here: Generics are not possible, sending the type as a parameter of type Type is not possible (see this post),... One last option is to write a method for each Enum I have (9 for the moment), but I got the feeling there's a better way than that.

Thanks in advance.

UPDATE: Using the solution provided by @KeithS I have following piece of code to create the dictionary:

Assembly asm = Assembly.GetExecutingAssembly();    
foreach (Type type in asm.GetTypes())
    {
        if (type.IsEnum)
        {
            enumTypeNames.Add(type.Name, type.AssemblyQualifiedName);
        }
    }

The method in the webservice returns a dict for every enummember for the given type:

public IDictionary<string, string> GetEnumDescriptionsList(string enumtypename)
{
    var enumtypenames = EnumMethods.GetEnumTypeNames();
    var returnvalue = new Dictionary<string, string>();

    string fullyQualifiedTypeName;
    enumtypenames.TryGetValue(enumtypename, out fullyQualifiedTypeName);

    var enumType = Type.GetType(fullyQualifiedTypeName);
    foreach (Enum value in Enum.GetValues(enumType))
    {
        returnvalue.Add(value.ToString(), value.GetDescription());
    }
    return returnvalue;
}

The call from the client:

var returnvalue = client.GetEnumDescriptionsList(typeof(UserType).Name);

Works like a charm, except for the issue I've posted a few days ago: Name of enum in innerclass changes through WCF-service

When I make a call from the client like:

var returnvalue = client.GetEnumDescriptionsList(typeof(ActionsMailDirectLinkContent).Name);

it gives the name "ActionsMailDirectLinkContent" back but in my dictionary of enumtypes I have "MailDirectLinkContent". So my question is if there's a way to avoid this or do I have to take my enum(s) out of the Actions-class as only solution.

UPDATE 2: As mentioned by @KeithS in his comment, I had to take the Enums out of my class. I have put them in a subfolder so they are still "grouped" but the methods above do work this way for all the Enums.

Thx Keith.

Community
  • 1
  • 1
Koen
  • 2,501
  • 1
  • 32
  • 43
  • 1
    Would an "enum of all available enum types" be workable here? – Marc Gravell Aug 08 '11 at 18:06
  • @Marc; I thought of suggesting that, but it would definitely require changing code in multiple places (and thus having to know you have to do it) when an Enum is added. Whether that's a big deal depends on various things beyond the scope of the question. – KeithS Aug 08 '11 at 18:31
  • @KeithS indeed; it might be great - it might be completely infeasible / inappropriate - hence a comment (question) not an answer ;p – Marc Gravell Aug 08 '11 at 18:55

3 Answers3

3

I have tried but still not get Description Attribute through WCF.

I have used like following

    [DataContract(Name = "HorseDNAKitRequest.RequestReasonSubType")]
    public enum RequestReasonSub : byte {
        [EnumMember(Value = "HERDA")]
        HERDA = 1,
        [EnumMember(Value = "HYPP")]
        HYPP = 2,
        [EnumMember(Value = "GBED")]
        GBED = 3,
        [EnumMember(Value = "OLWS")]
        OLWS = 4,
        [EnumMember(Value = "Red Factor And Agouti")]
        RedFactorandAgouti = 5,
        [EnumMember(Value = "Red Factor")]
        RedFactor = 6,
        [EnumMember(Value = "Agouti")]
        Agouti = 7,
        [EnumMember(Value = "Cream")]
        Cream = 8,
        [EnumMember(Value = "Pearl")]
        Pearl = 9,
        [EnumMember(Value = "Dun")]
        Dun,
        [EnumMember(Value = "Silver")]
        Silver,
        [EnumMember(Value = "Gray")]
        Gray,
        [EnumMember(Value = "Tobiano")]
        Tobiano,
        [EnumMember(Value = "Sabino")]
        Sabino,
    }

AND use a extension Method for Getting Value.

    public static string GetEnumDescription(this Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        EnumMemberAttribute[] attributes =
            (EnumMemberAttribute[])fi.GetCustomAttributes(
            typeof(EnumMemberAttribute),
            true);

        if (attributes != null && attributes.Length > 0)
            if (attributes[0].Value != null)
                return attributes[0].Value;
            else
                return value.ToString();
        else
            return value.ToString();
    }

It works perfectly

Yaseer Arafat
  • 81
  • 1
  • 4
3

If you can safely assume that the namespace is not required to make an Enum type's name unique (in other words there aren't and will not be two Enums named "MyEnum" that you're going to care about), you can set up a Dictionary<string,string> keyed to the "simple" (unqualified) Enum type name and containing the fully-qualified name of each Enum type on the server side. Then, you simply pass the simple Enum type name in to your method, and run it through the Dictionary to produce the "real" type name for which you get and return values, and any namespace differences between client and server won't matter.

The Dictionary could even be dynamically generated so it requires less maintenance, but you could start running into problems with scanning assemblies for Enum types (it would help if there was only one assembly or namespace that had all the Enums you care about).

KeithS
  • 70,210
  • 21
  • 112
  • 164
  • The enums are all located in the same assembly (even better: they are all in the same folder). Maybe it's a good idea to dynamically generate the dictionary, but I haven't got a clue how to accomplish that (sounds interesting, though). Or is the solution provided by @Marc in his comment a better one? Imho, changing code when adding a new enum is not a good idea when someone else has to do maintenance and "forget" to make the necessary changes. – Koen Aug 08 '11 at 18:46
  • If all the Enums you care about are always going to be in one project and folder (meaning they're all in one namespace), then it gets even easier; hard-code the namespace and/or assembly into the method. This is a much shakier assumption than the one I made in the answer, but if it's the way things work in your project then do it, possibly with a note or todo comment to keep an eye on this code. – KeithS Aug 09 '11 at 14:19
  • I've uipdated my question with the solution/idea you provided. I have created a static class with a static method in the same folder as the Enums. It works fine when I call my wcf-service with a "standard" enum, but it fails when I make the call with an enum that is inside a class. Any idea how to solve this or do I have to take this enum out of the class? – Koen Aug 09 '11 at 16:53
  • You'd have to take the Enum out of the class, as the enum is not visible from outside that class, meaning the Enum is dependent on part of its namespace, which breaks the assumptions made about the independence of the type from its namespace for identification. – KeithS Aug 09 '11 at 17:00
  • I thought so. The reason why this Enum is in another class is that there are going to be other Enums in the future that also represent an "Action" and I would have liked to group them all together. But I'm glad I managed to get the descriptions over WCF. Is the code I wrote ok, or are there things that can be written in a better way? – Koen Aug 09 '11 at 17:09
1

There is a trick, define your enum in this way in the contracts:

public enum EPaymentCycle
{
    [EnumMember(Value = "Month by Month")]
    Monthly,

    [EnumMember(Value = "Week by Week")]
    Weekly,

    [EnumMember(Value = "Hour by Hour")]
    Hours
}

The SvcUtils serialization produces an interesting result:

public enum EPaymentCycle : int
{

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Month by Month")]
    MonthByMonth= 0,

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Week by Week")]
    WeekbyWeek= 1,

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Hour by Hour")]
    HourbyHour = 2
}

You can read the EnumMemberAttribute Value by reflection and there you got it. Also the xsd metadata file produced by svcutil serialization is as expected:

<xs:simpleType name="EPaymentCycle">
<xs:restriction base="xs:string">
  <xs:enumeration value="Month by Month" />
  <xs:enumeration value="Week by Week" />
  <xs:enumeration value="Hour by Hour" />
</xs:restriction>

TommyGarcia
  • 119
  • 4