获取枚举描述以某种方式“一般地”超过WCF [英] Get Enum Description somehow "generically" over WCF

查看:115
本文介绍了获取枚举描述以某种方式“一般地”超过WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举,每个成员有一个 DescriptionAttribute
由于我无法通过WCF发送这个 DescriptionAttribute (学会了这个困难的方式),我一直在尝试创建一个发送我一个字典的方法与描述。
经过一段艰苦的努力,我在WCF服务中提出了这种方法:

  public IDictionary<枚举,字符串> GetEnumDescriptionsList(string enumtype)
{
类型t = Type.GetType(enumtype);
if(t.BaseType!= typeof(Enum))
{
return null;
}

var returnvalue = new Dictionary< Enum,string>();
foreach(Enum.GetValues(t)中的枚举值)
{
returnvalue.Add(value,value.GetDescription());
}
return returnvalue;
}

调用方法:

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

当我在与该方法相同的类中进行调用时,这可以正常工作,但是当我做出调用WCF我返回null。
调试完成后,我发现字符串参数持有不正确的值。我的意思是,客户端上的 UserType.ToString()具有不同的命名空间,然后是WCF服务中的一个。



阅读这篇文章解释了这个问题。 / p>

此刻我很困在这里:泛型是不可能的,发送类型为类型的参数类型是不可能的(看到这篇文章 )...
最后一个选项是为每个枚举写一个方法(目前为9),但我觉得有一种更好的方法。



提前谢谢



更新:
使用@KeithS提供的解决方案我有以下代码创建字典:

 程序集asm = Assembly.GetExecutingAssembly(); 
foreach(类型为asm.GetTypes())
{
if(type.IsEnum)
{
enumTypeNames.Add(type.Name,type.AssemblyQualifiedName );
}
}

webservice中的方法为每个枚举返回一个dict对于给定类型:

  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.GetValues(enumType)中的枚举值)
{
returnvalue.Add(value.ToString(),value.GetDescription());
}
return returnvalue;
}

来自客户端的电话:

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

除了前几天发布的问题,除了魅力外,还有一个魅力:通过WCF服务进行内部类更改的枚举名称



当我从客户端拨打电话时,如下所示:

  var returnvalue = client .GetEnumDescriptionsList(typeof运算(ActionsMailDirectLinkContent)请将.Name); 

它给出名称 ActionsMailDirectLinkContent 但在我的enumtypes字典中,我有 MailDirectLinkContent
所以我的问题是如果有办法避免这种情况,或者我必须将我的枚举从操作类作为唯一的解决方案。



更新2:
正如@KeithS在他的评论中所提到的,我不得不把Enums从我的课上拿出来。我把它们放在一个子文件夹中,所以它们仍然是分组的,但上面的方法对所有的枚举都是这样工作的。



Thx Keith。

解决方案

如果您可以安全地假设命名空间不是要求使枚举类型的名称是唯一的(换句话说,不存在或不会您可以设置一个名为MyEnum的枚举,您可以设置一个字典< string,string> 键入简单(不合格)枚举键入名称,并在服务器端包含每个枚举类型的完全限定名称。然后,您只需将简单的枚举类型名称传递给您的方法,并通过字典运行,以产生您获取并返回值的真实类型名称,客户端和服务器之间的任何命名空间差异都不重要。



字典甚至可以动态生成,所以它需要较少的维护,但是您可能开始遇到问题,扫描程序集枚举类型(如果只有一个装配或命名空间,具有您关心的所有枚举)。


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

这篇关于获取枚举描述以某种方式“一般地”超过WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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