Beispiel #1
0
        /// <summary>
        /// Constructs and instance of ConstructorArguments from a parsable object. This is the inverse of Construct().
        /// Note: will always set SubtypeName to the Type of object.
        /// </summary>
        /// <param name="obj">The object from which to construct the ConstructorArguments</param>
        /// <param name="suppressDefaults">Specifies whether values that are equal to the defaults should be included in the resulting ArgumentCollection</param>
        /// <returns>The result</returns>
        public static ConstructorArguments FromParsable(object obj, Type parseTypeOrNull = null, bool suppressDefaults = false)
        {
            ConstructorArguments constructor = new ConstructorArguments();

            constructor.SubtypeName = parseTypeOrNull == null || !parseTypeOrNull.Equals(obj.GetType()) ? obj.GetType().ToTypeString() : null;
            constructor.PopulateFromParsableObject(obj, suppressDefaults);
            return(constructor);
        }
Beispiel #2
0
            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 { }
                }
                else
                {
                    ConstructorArguments constLine = new ConstructorArguments(s);
                    try
                    {
                        t       = constLine.Construct <T>();
                        success = true;
                    }
                    catch (HelpException)
                    {
                        throw;
                    }
                    catch (ParseException)
                    {
                        throw;
                    }
                }

                return(success);
            }
Beispiel #3
0
        public static string ToParseString(this object o, Type parseTypeOrNull = null, bool suppressDefaults = false)
        {
            Type t = o.GetType();

            if (t.HasParseMethod() || !t.IsConstructable())
            {
                return(o.ToString());
            }
            else if (parseTypeOrNull == null)
            {
                return(ConstructorArguments.ToString(o));    // can only get here if t is constructable.
            }
            else
            {
                object valueAsParseType = ArgumentCollection.ImplicitlyCastValueToType(o, parseTypeOrNull);
                return(ConstructorArguments.ToString(valueAsParseType, suppressDefaults));
            }
        }
Beispiel #4
0
        /// <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>());
        }
Beispiel #5
0
        /// <summary>
        /// Constructs and instance of T, then runs it. This convenience method creates an instance of CommandArguments, then
        /// call ConstructAndRun on that result.
        /// </summary>
        /// <typeparam name="T">A //[Parsable] type that implements IExecutable.</typeparam>
        /// <param name="constructorString">Constructor arguments</param>
        public static void ConstructAndRun <T>(string constructorString) where T : IRunnable
        {
            ConstructorArguments command = new ConstructorArguments(constructorString);

            command.ConstructAndRun <T>();
        }