获取枚举说明莫名其妙"一般"在WCF [英] Get Enum Description somehow "generically" over WCF

查看:170
本文介绍了获取枚举说明莫名其妙"一般"在WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DescriptionAttribute 每个成员的枚举。
由于我不能够通过WCF发送此 DescriptionAttribute (学会了这艰难地),我一直在努力创建一个送我一本词典的方法与描述。
有点挣扎围绕之后,我拿出我的WCF服务这个方法:

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());

这时候我做在同一个班的方法调用工作正常,但是当我做在调用WCF我得到空返回。
调试后,我fnoticed该字符串参数持有不正确的价值。我的意思是 UserType.ToString()对疗法有客户方不同的命名空间,然后一个在WCF服务。

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.

帖子解释这个问题。

在这个时刻,我很坚持在这里:泛型是不可能的,送型类型的参数键入是不可能的(看到这个帖子 ),...
最后一个选择是编写每个枚举的时刻的方法我有(9),但我的感觉还有比这更好的办法。

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.

在此先感谢

更新:
使用由我有下面这段@KeithS提供的解决方案代码来创建字典:

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);
        }
    }

在Web服务的方法返回一个字典,每enummember对于给定类型:

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



工程就像一个魅力,除了我已经张贴在几天前问题:的在内部类的变化枚举通过WCF服务的名称

当我把从客户端像调用:

When I make a call from the client like:

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



它给名为 ActionsMailDirectLinkContent 回但在我enumtypes词典我有 MailDirectLinkContent
所以我的问题是,如果有一种方法来避免这个还是我把我的枚举(S)从操作级作为唯一的解决办法的。

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.

更新2:
正如在他的评论@KeithS提到的,我不得不采取枚举了我的班。我已经把它们的子目录下,所以他们仍然是分组,但上面的方法做工作,这种方式对于所有的枚举。

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基思。

推荐答案

如果你可以安全地假定命名空间是不需要做一个枚举类型的名称是唯一的(换句话说,有没有,将来也不会两个实例名为MyEnum那你要关心),你可以设置一个词典<字符串,字符串> 键入到简单(不合格)枚举键入名称和包含在服务器端的每个枚举类型的完全限定名称。然后,您只需在通过简单枚举类型名称的方法,并运行它通过字典来产生,而您获得和返回值真正的式的名称,客户端和服务器之间的任何名称空间的差异并不重要。

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

这篇关于获取枚举说明莫名其妙&QUOT;一般&QUOT;在WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆