/// <summary> /// To Parse String. /// </summary> /// <param name="obj">The Object.</param> /// <param name="parseTypeOrNull">Parse Type Or Null.</param> /// <param name="suppressDefaults">Suppress Defaults</param> /// <returns></returns> public static string ToParseString(this object obj, Type parseTypeOrNull = null, bool suppressDefaults = false) { if (obj == null) { return(null); } Type t = obj.GetType(); if (t.HasParseMethod() || !t.IsConstructable()) { return(obj.ToString()); } else if (obj is IEnumerable) { return("(" + ((IEnumerable)obj).StringJoin(",") + ")"); } else if (parseTypeOrNull == null) { return(ConstructorArguments.ToString(obj)); // can only get here if t is constructable. } else { object valueAsParseType = ArgumentCollection.ImplicitlyCastValueToType(obj, parseTypeOrNull); return(ConstructorArguments.ToString(valueAsParseType, suppressDefaults)); } }
/// <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="parseTypeOrNull">parseTypeOrNull</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 = true) { if (obj == null) { throw new ArgumentNullException("obj"); } ConstructorArguments constructor = new ConstructorArguments(); constructor.SubtypeName = parseTypeOrNull == null || !parseTypeOrNull.Equals(obj.GetType()) ? obj.GetType().ToTypeString() : null; constructor.PopulateFromParsableObject(obj, suppressDefaults); return(constructor); }
/// <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>()); }
/// <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>(); }