Exemplo n.º 1
0
            /// <summary>
            /// Try Parse.
            /// </summary>
            /// <param name="s"></param>
            /// <param name="t"></param>
            /// <returns></returns>
            public static bool TryParse(string s, out T t)
            {
                //Helper.CheckCondition(IsParsable(), "Cannot parse type {0}. It does not have a TryParse or Parse method defined", typeof(T));
                // now the general one.
                bool success = false;

                t = default(T);

                if (_tryParse != null)
                {
                    object[] args = new object[] { s, t };

                    success = (bool)_tryParse.Invoke(null, args);

                    if (success)
                    {
                        t = (T)args[1];
                    }
                }
                else if (_parse != null)
                {
                    try
                    {
                        object[] args = new object[] { s };
                        t       = (T)_parse.Invoke(null, args);
                        success = true;
                    }
                    catch (Exception) { }
                }
                else
                {
                    ConstructorArguments constLine = new ConstructorArguments(s);
                    try
                    {
                        t       = constLine.Construct <T>();
                        success = true;
                    }
                    catch (HelpException)
                    {
                        throw;
                    }
                    catch (ParseException)
                    {
                        throw;
                    }
                }

                return(success);
            }
        /// <summary>
        /// Simple wrapper that constructs an instance of type T from the command string.
        /// See ArgumentCollection.Construct() for documentation.
        /// </summary>
        /// <typeparam name="T">The Parsable type to be constructed</typeparam>
        /// <param name="commandString">The string from which to construct</param>
        /// <returns>The fully instantiated object</returns>
        public static T Construct <T>(string commandString)
        {
            ConstructorArguments command = new ConstructorArguments(commandString);

            return(command.Construct <T>());
        }