2016-02-28 98 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類。