2016-10-08 24 views
0

我的下面的代码工作正常,但是,我想尝试并改进其可移植性。 priceLevels索引号可能会有所变化,所以我不想更新程序,而是想设置一个设置选项来设置索引号。替代对象,而不是使用枚举索引值

我也希望能够控制PriceLevel类的ToString()方法。

PriceLevel类将是我Customers类的一部分,所以我的用法是这样的:

if(someOtherVariable == thisCustomer.priceLevel) //do some stuff 

someString = thisCustomer.priceLevel.ToString() 

-

public enum priceLevels 
{ 
    SELECT = 2, 
    PLUS = 3, 
    PREMIER = 3, 
    EFI = 4, 
    MSELECT = 5, 
    SPECIAL = 6 
} 

class PriceLevel 
{ 
    public priceLevels priceLevel { get; set; } 

    public override string ToString() 
    { 
     string myString = "No Level Set"; 

     if (priceLevel == priceLevels.SELECT) return "Partner-Select"; 
     if (priceLevel == priceLevels.PLUS) return "Plus/Premier"; 
     if (priceLevel == priceLevels.PREMIER) return "Plus/Premier"; 
     if (priceLevel == priceLevels.EFI) return "eFi Plus-SPA"; 
     if (priceLevel == priceLevels.MSELECT) return "mSelect"; 
     if (priceLevel == priceLevels.SPECIAL) return "Special"; 

     return myString; 
    } 
} 

会有人能够提出一个替代的对象,我可以用来代替priceLevels枚举?

回答

2

我不知道,如果你使用priceLevels别的东西。因为如果你改变PriceLevel类或删除priceLevels枚举,meybe您需要更改Customer类过或方式如何保存的价格水平值。

如果你保持priceLevels枚举。您可以使用attibute说明了这一点,并在您的项目包括一个简单的扩展:

public enum priceLevels 
{ 
    [Description("Partner-Select")] 
    SELECT = 2, 
    [Description("...")] 
    PLUS = 3, 
    [Description("...")] 
    PREMIER = 3, 
    [Description("...")] 
    EFI = 4, 
    [Description("...")] 
    MSELECT = 5, 
    [Description("...")] 
    SPECIAL = 6 
} 

public static string GetDescription(this Enum enumValue) 
{ 
    var fi = enumValue.GetType().GetField(enumValue.ToString()); 

    var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 

    return (attributes != null && attributes.Length > 0) 
       ? attributes[0].Description 
       : enumValue.ToString(); 
} 

用途:

someString = thisCustomer.priceLevel.priceLevel.GetDescription() 

,或者您可以将属性PriceLevel与类型更改Customer类,并包括在此priceLevels

someString = thisCustomer.PriceLevel.GetDescription() 

当我用这个,我somethimes包括资源文件。因为我可以在这个资源中保存稀疏的描述。

+0

谢谢,我不认为我需要的'PriceLevel'其他地方,所以我觉得这是很有道理的添加财产给我的班。至于我需要能够改变枚举的索引,我想我会用它来对应一个具有默认值的数组,然后我可以只有一个设置文件,如果它有一些其他的值枚举它将改变数组中的值。 – Adjit

1

如果主理由不使用EnumToString(),可以通过添加在该部件上的属性和使用的扩展方法作为@andres建议重写的结果;如果你想要一个更复杂的结构,你可以模拟一个枚举,并添加相应的功能:

public struct PriceLevels 
{ 
    public static PriceLevels NONE = 0; 

    public static PriceLevels SELECT = 2; 
    public static PriceLevels PLUS = 3; 
    public static PriceLevels PREMIER = 3; 
    public static PriceLevels EFI = 4; 
    public static PriceLevels MSELECT = 5; 
    public static PriceLevels SPECIAL = 6; 

    public bool Equals(PriceLevels other) => _number == other._number; 

    public override bool Equals(object obj) => !ReferenceEquals(null, obj) && obj is PriceLevels && Equals((PriceLevels) obj); 

    public override int GetHashCode() => _number; 

    readonly int _number; 

    PriceLevels(int number) 
    { 
     _number = number; 
    } 

    public static implicit operator PriceLevels(int number) => new PriceLevels(number); 

    public static bool operator ==(PriceLevels leftLevel, PriceLevels rightLevel) => leftLevel._number == rightLevel._number; 

    public static bool operator !=(PriceLevels leftLevel, PriceLevels rightLevel) => !(leftLevel == rightLevel); 

    public override string ToString() 
    { 
     if (this == SELECT) return "Partner-Select"; 
     if (this == PLUS) return "Plus/Premier"; 
     if (this == PREMIER) return "Plus/Premier"; 
     if (this == EFI) return "eFi Plus-SPA"; 
     if (this == MSELECT) return "mSelect"; 
     if (this == SPECIAL) return "Special"; 

     return "No Level Set"; 
    } 
}