public static void Parse(string input, ITimeSpan expectedTimeSpan) { TimeSpanUtilities.TryParse(input, out var actualTimeSpan); Assert.AreEqual(expectedTimeSpan, actualTimeSpan, "TryParse: incorrect result."); actualTimeSpan = TimeSpanUtilities.Parse(input); Assert.AreEqual(expectedTimeSpan, actualTimeSpan, "Parse: incorrect result."); Assert.AreEqual(expectedTimeSpan, TimeSpanUtilities.Parse(expectedTimeSpan.ToString()), "Parse: string representation was not parsed to the original time span."); }
internal static object ConvertParameterStringToObject(Type desiredType, string parameters, object defaultObject) { try { //DO NOT ADD CUSTOM CLASSES TO CONVERT TYPE! USE A DATABASE PRIMARY KEY INSTEAD! //System Objects #region Boolean if (desiredType == typeof(Boolean)) { Boolean output; return(Boolean.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Byte if (desiredType == typeof(Byte)) { Byte output; return(Byte.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region SByte if (desiredType == typeof(SByte)) { SByte output; return(SByte.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Int16 if (desiredType == typeof(Int16)) { Int16 output; return(Int16.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region UInt16 if (desiredType == typeof(UInt16)) { UInt16 output; return(UInt16.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Int32 if (desiredType == typeof(Int32)) { Int32 output; return(Int32.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region UInt32 if (desiredType == typeof(UInt32)) { UInt32 output; return(UInt32.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Int64 if (desiredType == typeof(Int64)) { Int64 output; return(Int64.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region UInt64 if (desiredType == typeof(UInt64)) { UInt64 output; return(UInt64.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Single if (desiredType == typeof(Single)) { Single output; return(Single.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region Double if (desiredType == typeof(Double)) { Double output; return(Double.TryParse(parameters, out output) ? output : defaultObject); } #endregion #region IPAddress if (desiredType == typeof(IHostAddress)) { IHostAddress Default = defaultObject as IHostAddress; try { IHostAddress HostAddress = ObjectFactory.CreateHostAddress(parameters); return((Default.IpAddress.ToString() == HostAddress.IpAddress.ToString()) ? Default : HostAddress); } catch { } return(defaultObject); } #endregion #region String if (desiredType == typeof(String)) { return(parameters); } #endregion //Units Of Measurement #region Angle if (desiredType == typeof(IAngle)) { IAngle nullConversion; ObjectFactory.TryParse("0DEGREES", out nullConversion); IAngle converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Area if (desiredType == typeof(IArea)) { IArea nullConversion; ObjectFactory.TryParse("0SQUAREMETERS", out nullConversion); IArea converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Distance if (desiredType == typeof(IDistance)) { IDistance nullConversion; ObjectFactory.TryParse("0METERS", out nullConversion); IDistance converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Durations if (desiredType == typeof(IDate)) { IDate nullConversion = ObjectFactory.CreateDate(""); IDate converted = ObjectFactory.CreateDate(parameters); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } if (desiredType == typeof(IDateTime)) { IDateTime nullConversion = ObjectFactory.CreateDateTime(""); IDateTime converted = ObjectFactory.CreateDateTime(parameters); return((converted.ToString() == nullConversion.ToSystemString()) ? defaultObject : converted); } if (desiredType == typeof(ITime)) { ITime nullConversion = ObjectFactory.CreateTime(""); ITime converted = ObjectFactory.CreateTime(parameters); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } if (desiredType == typeof(ITimeSpan)) { ITimeSpan nullConversion = ObjectFactory.CreateTimeSpan(""); ITimeSpan converted = ObjectFactory.CreateTimeSpan(parameters); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Energy if (desiredType == typeof(IEnergy)) { IEnergy nullConversion; ObjectFactory.TryParse("0KILOJOULES", out nullConversion); IEnergy converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Mass if (desiredType == typeof(IMass)) { IMass nullConversion; ObjectFactory.TryParse("0KILOGRAMS", out nullConversion); IMass converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Power if (desiredType == typeof(IPower)) { IPower nullConversion; ObjectFactory.TryParse("0KILOWATTS", out nullConversion); IPower converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Pressure if (desiredType == typeof(IPressure)) { IPressure nullConversion; ObjectFactory.TryParse("0PASCALS", out nullConversion); IPressure converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Speed if (desiredType == typeof(ISpeed)) { ISpeed nullConversion; ObjectFactory.TryParse("0M/SEC", out nullConversion); ISpeed converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Temperature if (desiredType == typeof(ITemperature)) { ITemperature nullConversion; ObjectFactory.TryParse("0CELCIUS", out nullConversion); ITemperature converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion #region Volume if (desiredType == typeof(IVolume)) { IVolume nullConversion; ObjectFactory.TryParse("0LITRES", out nullConversion); IVolume converted; ObjectFactory.TryParse(parameters, out converted); return((converted.ToString() == nullConversion.ToString()) ? defaultObject : converted); } #endregion //Colors #region XRGBColor if (desiredType == typeof(I24BitColor)) { string[] split = parameters.Split(' '); byte red = 255; byte green = 255; byte blue = 255; I24BitColor Default = defaultObject as I24BitColor; if (split.Length < 3) { return(Default); } try { red = Convert.ToByte(split[0]); green = Convert.ToByte(split[1]); blue = Convert.ToByte(split[2]); } catch (OverflowException) { //Debug return(Default); } catch (FormatException) { //Debug return(Default); } I24BitColor output = ObjectFactory.CreateColor(red, green, blue).Get24BitColor(); return(output); } #endregion } catch (Exception e) { Debug.AddErrorMessage(e, "Error converting setting from string."); } return(defaultObject); }