2016-02-28 97 views
1

我正在用实体框架构建模型并购买了响应式CSS。实体框架中的枚举对象(字符串)

内置固定图标附带CSS。就像如下(Name and Icon Class Value)

enter image description here

我需要一种方法来保持图标的名称为固定枚举VS智能感知访问它。目前,我们无法将其作为实体表存储在实体框架中(因为它需要与难以维护的表的关系),并且枚举不允许使用字符串类型。

代码没有奏效:

public sealed class IconType 
    { 
     public static readonly IconType Rupee_Icon = new IconType("rupee-icons"); 
     public static readonly IconType Doller_Icon = new IconType("doller-icon"); 

     private IconType(int EnumID,string EnumObjectValue) 
     {    
      IconValue = EnumObjectValue; 
     } 

     public string IconValue { get; private set; } 
    } 

更多的代码,没有工作(CSS类名包含像ui bell icon空格):

public enum Icon 
{ 
     NotSet=0, 
     Idea Icon=1, 
     Bell Icon =2 
} 

是否有任何其他方式使用名称/对象作为在EF中的枚举或常量在Visual Studio中轻松实现intellisense?

回答

0

,你可以:

  1. 忽略的空格在枚举:

    public enum Icon 
    { 
        NotSet = 0, 
        IdeaIcon = 1, 
        BellIcon = 2 
    } 
    
  2. 添加说明或名称(或者甚至是一些自定义属性)属性的枚举:

    public enum Icon 
    { 
        NotSet = 0, 
    
        [Description("ui idea icon")] 
        IdeaIcon = 1, 
    
        [Description("ui bell icon")] 
        BellIcon = 2 
    } 
    
  3. 当需要时获取描述名称。例如方法获取描述属性值:

    public static string GetDescription<T>(this T enumerationValue) 
        where T : struct, IConvertible 
    { 
        var type = enumerationValue.GetType(); 
        if (!type.IsEnum) 
        { 
         throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue"); 
        } 
    
        // Tries to find a DescriptionAttribute for a potential friendly name for the enum 
        var memberInfo = type.GetMember(enumerationValue.ToString(CultureInfo.InvariantCulture)); 
        if (memberInfo.Length > 0) 
        { 
         var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); 
         if (attributes.Length > 0) 
         { 
          // Pull out the description value 
          return ((DescriptionAttribute)attributes[0]).Description; 
         } 
        } 
    
        // If we have no description attribute, just return the ToString of the enum 
        return enumerationValue.ToString(CultureInfo.InvariantCulture); 
    } 
    
0

你有没有考虑使用字符串常量?

public static class IconType 
{ 
    public const string RUPEE_ICON = "rupee-icon"; 
    public const string DOLLER_ICON = "doller-icon"; 
    // ... 
} 
0

将图标存储为普通旧对象。为什么要使用实体框架呢?

public static class Icons 
{ 
    public enum Type 
    { 
     IdeaIcon = 1, 
     BellIcon =2 
    } 

    public static Icon Get(Type type) 
    { 
     return IconCollection.Single(icon => icon.Type == type); 
    } 

    static IEnumerable<Icon> IconCollection 
    { 
     get 
     { 
      return new List<Icon> 
      { 
       new Icon(Type.IdeaIcon, "Idea Icon", "icon idea-icon"), 
       new Icon(Type.BellIcon, "Bell Icon", "icon bell-icon"), 
      }; 
     } 
    } 

    public class Icon 
    { 
     public Icon(Type type, string description, string cssClass) 
     { 
      Type = type; 
      Description = description; 
      CssClass = cssClass; 
     } 
     public Type Type { get; private set; } 
     public string Description { get; private set; } 
     public string CssClass { get; private set; } 
    } 
} 

使用代码:

public class Class1 
{ 
    public void Method1() 
    { 
     var ideaIcon = Icons.Get(Icons.Type.IdeaIcon); 
     var x = ideaIcon.CssClass; 
     var y = ideaIcon.Description; 

     var bellIcon = Icons.Get(Icons.Type.BellIcon); 
     // etc... 
    } 
} 

Razor视图:

@Icons.Get(Icons.Type.BellIcon).CssClass 

如果您需要枚举的图标集合,你可以轻松地添加其他静态访问到Icons类。