代码之家  ›  专栏  ›  技术社区  ›  sipsorcery

将字符串属性限制为预定义的值集(解决枚举中没有连字符的问题)

  •  2
  • sipsorcery  · 技术社区  · 15 年前

    我有一个字符串属性,我希望能够强制使用以下两项: -它只能设置为预定义列表中的特定值, -可以在编译时执行属性值的错误检查。

    枚举完全符合该清单,但在我的预定义字符串列表中,有一个带有连字符和 enum values cannot contain hyphens . 为了说明理想的解决方案,如果枚举可以包含连字符,我将创建以下枚举:

    public enum SIPEventPackagesEnum
    {
        dialog,
        message-summary,
        refer
    }
    

    使用:

    SIPEventPackagesEnum EventPackage = SIPEventPackagesEnum.message-summary;
    

    设置:

    string eventPackageStr = "message-summary";
    SIPEventPackagesEnum EventPackage = (SIPEventPackagesEnum)Enum.Parse(typeof(SIPEventPackagesEnum), eventPackageStr, true);
    

    在上述情况下,不可能将EventPackage属性设置为除一个枚举值以外的任何值,而且使用起来很直观,因为IntelliSense将列出可用选项。

    由于无法使用连字符,并且我无法更改预定义的列表以删除连字符,因此非常粗糙的方法是使用结构,并在结构上具有“value”属性,该属性在其setter中执行,请参见下文。与使用枚举相比,它非常冗长,而且不允许任何编译时检查,也不太直观。

    以前有人遇到过这个问题吗?有更好的解决方案吗?我有多个包含连字符的项目列表,所以它不是一次性的。

    public struct SIPEventPackages
    {
        public const string DIALOG = "dialog";                   
        public const string MESSAGE_SUMMARY = "message-summary";   
        public const string REFER = "refer";                      
    
        public string Value
        {
            get { return Value; }
            set
            {
                if (IsValid(value))
                {
                    Value = value.ToLower();
                }
                else
                {
                    throw new ArgumentException(value + " is invalid for a SIP event package.");
                }
            }
        }
    
        public bool IsValid(string value)
        {
            if (value.IsNullOrBlank())
            {
                return false;
            }
            else if (value.ToLower() == DIALOG || value.ToLower() == MESSAGE_SUMMARY || value.ToLower() == REFER)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        public override string ToString()
        {
            return Value;
        }
    }
    
    3 回复  |  直到 15 年前
        1
  •  0
  •   RameshVel    15 年前

            Func<string, bool> isValid = str =>
            {
                List<string> validLst = new List<string>() { "dialog","message-summary","refer" };
                if (validLst.Find(x => string.Equals(x,str,StringComparison.InvariantCultureIgnoreCase)) == null)
                    return false;
                return true;
    
            };
    
            var teststr1 = "message-summary";
            var teststr2 = "wrongone";
            isValid(teststr1);
            isValid(teststr2);
    

        public enum SIPEventPackagesEnum
        {
            dialog,
            messagesummary,
            refer
        }
    
        string eventPackageStr = "message-summary";
        SIPEventPackagesEnum EventPackage = (SIPEventPackagesEnum)Enum.Parse(typeof(SIPEventPackagesEnum), eventPackageStr.Replace("-",""), true);
    
        2
  •  0
  •   thecoop    15 年前

    Dictionary<SIPEventPackagesEnum ,string> Enum.Parse(typeof(SIPEventPackagesEnum), str.Replace("-", ""))

        3
  •  0
  •   sipsorcery    15 年前

    public struct SIPEventPackage
    {
        public static SIPEventPackage None = new SIPEventPackage(null);
        public static SIPEventPackage Dialog = new SIPEventPackage("dialog");                   
        public static SIPEventPackage MessageSummary = new SIPEventPackage("message-summary"); 
        public static SIPEventPackage Refer = new SIPEventPackage("refer");                    
    
        private string m_value;
    
        private SIPEventPackage(string value)
        {
            m_value = value;
        }
    
        public override string ToString()
        {
            return m_value;
        }
    
        public static SIPEventPackage Parse(string value)
        {
            if (!IsValid(value))
            {
                throw new ArgumentException("The value is not valid for a SIPEventPackage.");
            }
            else
            {
                string trimmedValue = value.Trim().ToLower();
                switch (trimmedValue)
                {
                    case "dialog": return SIPEventPackage.Dialog;
                    case "message-summary": return SIPEventPackage.MessageSummary;
                    case "refer": return SIPEventPackage.Refer;
                    default: throw new ArgumentException("The value is not valid for a SIPEventPackage.");
                }
            }
        }
    }