枚举多描述 [英] Enum with multiple descriptions

查看:92
本文介绍了枚举多描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的枚举是这样的。

 公开枚举性别
{
    男人= 1,
    女人= 2
}

和我使用ASP MVC4在下降这样的下降显示选项。

  @ Html.DropDownListFor(型号=> model.Gender,新的SelectList(Enum.GetValues​​(typeof运算(Namespace.Models.Enum.Gender))))

这就像一个魅力,它在下拉显示男人/女人了。
我的问题是,我想展示在不同背景下的枚举不同的名称。

如果你是一个妈妈或爸爸

就像一个上下文会。我想用性别枚举为基础,但显示的妈妈/爸爸instad男人/女人。

另一个方面是男孩/女孩,我还是想用性别枚举,但显示不同的文本。

以任何方式这可能吗?

修改
我用凯文的解决方案,还添加了这样的另一种延伸方法。

 公共静态列表< KeyValuePair<字符串,整数>>的GetValues​​(IGenderStrategy genderStrategy)
    {
        字典<字符串,整数> ARR =新词典<字符串,整数>();        的foreach(在System.Enum.GetValues​​性别克(typeof运算(性别)))
            arr.Add(g.ToValue(genderStrategy),(INT)G);        返回arr.ToList();
    }

这在我以前像这样我的看法。

  @ Html.DropDownListFor(型号=> model.Gender,新的SelectList(Chores.Models.Enum.EnumExtentions.GetValues​​(新Chores.Models.Enum.ParentStrategy()), 值,键))


解决方案

我喜欢使用扩展方法@RakotVT的答案,但会有点进一步扩展它,你需要每一种情况是不是很大一个新的扩展方法。

我觉得战略格局的变化可能会更好地工作,这里( HTTP://www.dofactory .COM /模式/ PatternStrategy.aspx

事情是这样的 -

 公开枚举性别
{
    男人= 1,
    女人= 2
}公共接口IGenderStrategy
{
    字符串显示名称(性别性别);
}公共类ParentStrategy:IGenderStrategy
{
    公共字符串显示名称(性别性别)
    {
        字符串retVal的=的String.Empty;
        开关(性别)
        {
            案例Gender.Man:
                retVal的=爸爸;
                打破;
            案例Gender.Woman:
                retVal的=妈妈;
                打破;
            默认:
                抛出新的异常(性别没有找到);
        }
        返回retVal的;
    }
}公共静态类EnumExtentions
{
    公共静态字符串ToValue(这种性别E,IGenderStrategy genderStategy)
    {
        返回genderStategy.DisplayName(E);
    }
}公共类测试
{
    公开测试()
    {
        Gender.Man.ToValue(新ParentStrategy());
    }
}

I have my enum like this.

public enum Gender
{
    Man = 1,
    Woman = 2
}

And I use ASP MVC4 to display the choices in a drop down like this.

@Html.DropDownListFor(model => model.Gender, new SelectList(Enum.GetValues(typeof(Namespace.Models.Enum.Gender))))

This works like a charm, it display Man/Woman in the drop down. My problem is that I would like to show different names on those enums in different contexts.

Like one context would be if you are a Mom or a Dad. I would like to use the gender enum as base, but display Mom/Dad instad of Man/Woman.

Another context would be Boy/Girl, I still would like to use the gender enum, but display a different text.

Is this possible in any way?

EDIT I used Kevin's solution and also added another extention method like this.

public static List<KeyValuePair<string, int>> GetValues(IGenderStrategy genderStrategy)
    {
        Dictionary<string, int> arr = new Dictionary<string, int>();

        foreach (Gender g in System.Enum.GetValues(typeof(Gender)))
            arr.Add(g.ToValue(genderStrategy), (int)g);

        return arr.ToList();
    }

Which I used like this in my view.

@Html.DropDownListFor(model => model.Gender, new SelectList(Chores.Models.Enum.EnumExtentions.GetValues(new Chores.Models.Enum.ParentStrategy()), "value", "key"))

解决方案

I like @RakotVT answer of using an extension method but would extend it a bit further as you would need a new extension method for every situation which is not great.

I think a variation of the Strategy pattern might work better here (http://www.dofactory.com/Patterns/PatternStrategy.aspx)

Something like this -

public enum Gender
{
    Man = 1,
    Woman = 2
}

public interface IGenderStrategy
{
    string DisplayName(Gender gender);
}

public class ParentStrategy : IGenderStrategy
{
    public string DisplayName(Gender gender)
    {
        string retVal = String.Empty;
        switch (gender)
        {
            case Gender.Man:
                retVal =  "Dad";
                break;
            case Gender.Woman:
                retVal =  "Mom";
                break;
            default:
                throw new Exception("Gender not found");
        }
        return retVal;
    }
}

public static class EnumExtentions
{
    public static string ToValue(this Gender e, IGenderStrategy genderStategy)
    {
        return genderStategy.DisplayName(e);
    }
}

public class Test
{
    public Test()
    {
        Gender.Man.ToValue(new ParentStrategy());
    }
}

这篇关于枚举多描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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