///////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Parses input text to enum type element.
        /// </summary>
        /// <param name="type">Application enum type.</param>
        /// <param name="text">Input text to parsing.</param>
        /// <returns>Enum type element</returns>
        static public object Parse(Type type, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException(); // exception
            }
            if (!type.IsEnum)
            {
                throw new ArgumentException(); // exception
            }
            // NOTE: conversion supported only for Enums

            string normText = CommonHelpers.NormalizeText(text);

            object result = null;

            // check in predefined strings from resource
            Array elements = Enum.GetValues(type);

            foreach (object element in elements)
            {
                string[] supportedSymbols = _GetSupportedValues(element);
                if (CommonHelpers.IsValuePresentInList(normText, supportedSymbols))
                {
                    result = element;
                    break; // NOTE: result founded.
                }
            }

            // try convert as index
            if (null == result)
            {
                int index = int.Parse(normText);
                if (elements.Length <= index)
                {
                    throw new InvalidOperationException(); // exception
                }
                int curIndex = 0;
                foreach (object element in elements)
                {
                    if (index == curIndex)
                    {
                        result = element;
                        break; // operation done - stop
                    }
                    ++curIndex;
                }
            }

            // must be inited
            if (null == result)
            {
                throw new InvalidOperationException(); // exception
            }
            return(result);
        }
示例#2
0
        /// <summary>
        /// Gets unit by symbol.
        /// </summary>
        /// <param name="symbol">Input symbol text.</param>
        /// <param name="units">Units to check.</param>
        /// <returns>Parsed unit type.</returns>
        public static Unit _GetUnitBySymbol(string symbol, Unit[] units)
        {
            if (string.IsNullOrEmpty(symbol.Trim()))
            {
                throw new System.ArgumentException(); // exception
            }
            string normSymbol    = CommonHelpers.NormalizeText(symbol);
            string symbolToCheck = _RemoveDots(normSymbol);

            Unit result = Unit.Unknown;

            for (int index = 0; index < units.Length; ++index)
            {
                Unit     unit             = units[index];
                string[] supportedSymbols = _GetSupportedSymbols(unit);
                if (CommonHelpers.IsValuePresentInList(symbolToCheck, supportedSymbols))
                {
                    result = unit;
                    break; // NOTE: result founded.
                }
            }

            return(result);
        }