2011-07-19 51 views
0

我枚举结构是这样的如何读取ASP.Net中的枚举值?

public enum UserRole 
{ 
    Administrator = "Administrator", 
    Simple_User = "Simple User", 
    Paid_User = "Paid User" 
} 

现在我想读通过使用其名称此枚举值假设

String str = UserRole.Simple_User; 

它给了我“简单的用户”在海峡代替“Simple_User”

我们如何做到这一点???

+0

为什么你想要Simple_User intead吗?如果你确实为什么不改变枚举值?无论如何你想完成什么,因为它可能是你想要使用一个枚举,你可能需要别的东西,需要更多的上下文。 –

+2

你的代码不能编译... –

+0

解释一下,你是如何编译这段代码并产生不需要的结果的? :-) –

回答

3

你可以做一个友好的描述,像这样:

public enum UserRole 
    { 
     [Description("Total administrator!!1!one")] 
     Administrator = 1, 

     [Description("This is a simple user")] 
     Simple_User = 2, 

     [Description("This is a paid user")] 
     Paid_User = 3, 
    } 

并做好辅助函数:

public static string GetDescription(Enum en) 
     { 
      Type type = en.GetType(); 

      MemberInfo[] info = type.GetMember(en.ToString()); 

      if (info != null && info.Length > 0) 
      { 
       object[] attrs = info[0].GetCustomAttributes(typeof(DescriptionAttribute), false); 

       if (attrs != null && attrs.Length > 0) 
       { 
        return ((DescriptionAttribute)attrs[0]).Description; 
       } 
      } 

      return en.ToString(); 
     } 

而且使用它像:

string description = GetDescription(UserRole.Administrator); 
1

嗯,枚举不能有字符串值。从MSDN's Enum page

枚举是一组命名常量,其基础类型是除Char以外的任何整型。


要获得枚举的字符串版本使用枚举的方法ToString

String str = UserRole.Simple_User.ToString("F"); 
+0

它不工作枚举给我错误“不能隐式转换类型'字符串'为'int'”和我在MSDN看到枚举是一个字符串和int组合的集合而不是一个字符串字符串组合... –

+0

@Gaurav枚举的不能是字符串。你的'UserRole'枚举不起作用。 –

0

我有点困惑你的问题,因为C#不允许你声明由字符串支持的枚举。你的意思是你想要得到“简单用户”,但你得到“Simple_User”,而不是?

如何:

var str = UserRole.Simple_User.ToString().Replace("_", " "); 
0

您可以通过属性做到这一点,但实际上我认为使用一个枚举像这可能是错误的方式去了解的东西。

我会创建一个暴露显示名称属性的用户角色接口,然后为每个角色实现一个新类的接口,这也让您在将来添加更多行为。 或者你可以使用一个抽象类,所以任何一般的行为没有得到复制...

2

好了,现在你知道真正的枚举LY是,你可以给一个方便的字符串句柄号码清单喜欢:

public enum ErrorCode 
{ 
    CTCLSM = 1, 
    CTSTRPH = 2, 
    FBR = 3, 
    SNF = 4 
} 

而且,@StriplingWarrior显示,你可以走这么远,通过获取枚举字符串名称和更换下划线等,但我认为你想要的是将一个漂亮的人字符串与每个值相关联的方式。这个怎么样?

public enum ErrorCode 
{ 
    [EnumDisplayName("Cataclysm")] 
    CTCLSM = 1, 
    [EnumDisplayName("Catastrophe")] 
    CTSTRPH = 2, 
    [EnumDisplayName("Fubar")] 
    FBR = 3, 
    [EnumDisplayName("Snafu")] 
    SNF = 4 
} 

好了有可能是一些在System.ComponentModel,它这一点 - 让我知道。我的解决方案的代码是在这里:

[AttributeUsage(AttributeTargets.Field)] 
public class EnumDisplayNameAttribute : System.Attribute 
{ 
    public string DisplayName { get; set; } 

    public EnumDisplayNameAttribute(string displayName) 
    { 
     DisplayName = displayName; 
    } 
} 

而时髦的枚举扩展,使得它可能:

public static string PrettyFormat(this Enum enumValue) 
{ 
    string text = enumValue.ToString(); 
    EnumDisplayNameAttribute displayName = (EnumDisplayNameAttribute)enumValue.GetType().GetField(text).GetCustomAttributes(typeof(EnumDisplayNameAttribute), false).SingleOrDefault(); 

    if (displayName != null) 
     text = displayName.DisplayName; 
    else 
     text = text.PrettySpace().Capitalize(true); 

    return text; 
} 

因此得到人性化的价值了,你可以只是做ErrorCode.CTSTRPH.PrettyFormat()