using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lobster.Components { public class TStringToEnumConverter<TEnum> where TEnum : struct { private bool _bCaseSensitive = false; private TEnum _defaultValue; private Dictionary<string, TEnum> dic; public TStringToEnumConverter(TEnum defaultValue, bool bCaseSensitive) { if (!typeof(TEnum).IsEnum) { throw new ArgumentException("TEnum must be an enumeration"); } _defaultValue = defaultValue; _bCaseSensitive = bCaseSensitive; var values = Enum.GetValues(typeof(TEnum)).Cast<TEnum>(); dic = new Dictionary<string, TEnum>(); foreach (var v in values) { dic.Add(_bCaseSensitive ? v.ToString() : v.ToString().ToLower(), v); } } public TEnum this[string index] { get { if (!_bCaseSensitive) index = index.ToLower(); if (dic.ContainsKey(index)) return dic[index]; return _defaultValue; } } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)