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.
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).