public static object ConvertTo(string value, System.Type targetType) { switch (targetType.FullName) { case "System.String": return(value); case "System.Char": if (value.Length == 1) { return(Convert.ToChar(value)); } throw ThrowHelper.New <InvalidFieldValueException>(ExceptionContext, ErrorMessages.InvalidCharValue, value); case "System.Boolean": if (TrueWireValueString.Equals(value) || Boolean.TrueString.Equals(value, StringComparison.CurrentCultureIgnoreCase)) { return(true); } if (FalseWireValueString.Equals(value) || Boolean.FalseString.Equals(value, StringComparison.CurrentCultureIgnoreCase)) { return(false); } throw ThrowHelper.New <InvalidFieldValueException>(ExceptionContext, ErrorMessages.InvalidBooleanValue, value, TrueWireValueString, FalseWireValueString); case "System.Int32": return(Convert.ToInt32(value, CultureInfo.InvariantCulture)); case "System.Decimal": return(Convert.ToDecimal(value, CultureInfo.InvariantCulture)); case "System.DateTime": { DateTime result; if (!FixDateTime.TryParse(value, CultureInfo.InvariantCulture, out result)) { throw ThrowHelper.New <InvalidFieldValueException>(ExceptionContext, ErrorMessages.InvalidDateOrTimeValue, value); } return(result); } case "Atdl4net.Fix.FixTag": return(new FixTag(Convert.ToInt32(value, CultureInfo.InvariantCulture))); default: if (targetType.FullName.StartsWith("Atdl4net.Model.Controls.InitValue")) { return(value); } else { throw ThrowHelper.New <InternalErrorException>(ExceptionContext, InternalErrors.UnrecognisedAttributeType, targetType.FullName); } } }
/// <summary> /// Attempts to load the supplied FIX field value into this control. /// </summary> /// <param name="value">Value to set this control to.</param> /// <returns>true if it was possible to set the value of this control using the supplied value; false otherwise.</returns> protected override bool LoadDefaultFromFixValue(string value) { DateTime result; bool parsed = FixDateTime.TryParse(value, CultureInfo.InvariantCulture, out result); _value = parsed ? (DateTime?)result : null; return(parsed); }
/// <summary> /// Attempts to convert the second parameter value to a comparable type of the first parameter. /// </summary> /// <param name="typeInstanceToMatch">Instance of a the target comparable type.</param> /// <param name="value">Value to convert.</param> /// <returns>Converted value as an <see cref="IComparable"/>.</returns> /// <exception cref="InvalidCastException">Thrown if the value cannot be converted to the target type.</exception> /// <exception cref="FormatException">Thrown if the value cannot be converted into a valid numeric type.</exception> public static IComparable ConvertToComparableType(object typeInstanceToMatch, string value) { // If we don't have a valid type to convert to, then best leave the value alone. if (typeInstanceToMatch == null) { return(value); } string type = typeInstanceToMatch.GetType().FullName; switch (type) { case "System.Decimal": return(Convert.ToDecimal(value)); case "System.Boolean": return(ConvertToBool(value)); case "System.Int32": return(Convert.ToInt32(value)); case "System.UInt32": return(Convert.ToUInt32(value)); case "System.Char": return(Convert.ToChar(value)); case "System.DateTime": return(FixDateTime.Parse(value, CultureInfo.InvariantCulture)); case "System.String": return(value); case "Atdl4net.Model.Reference.IsoCountryCode": return(value.ParseAsEnum <IsoCountryCode>()); case "Atdl4net.Model.Reference.IsoCurrencyCode": return(value.ParseAsEnum <IsoCurrencyCode>()); case "Atdl4net.Model.Reference.IsoLanguageCode": return(value.ParseAsEnum <IsoLanguageCode>()); case "Atdl4net.Model.Types.Support.MonthYear": return(MonthYear.Parse(value)); case "Atdl4net.Model.Types.Support.Tenor": return(Tenor.Parse(value)); case "Atdl4net.Model.Controls.Support.EnumState": return(value); default: throw ThrowHelper.New <InvalidCastException>(ExceptionContext, ErrorMessages.DataConversionError1, value, type); } }
/// <summary> /// Converts the value of this instance to an equivalent nullable DateTime value using the specified culture-specific formatting information. /// </summary> /// <param name="targetParameter">Target parameter for this conversion.</param> /// <param name="provider">An <see cref="IFormatProvider"/> interface implementation that supplies culture-specific formatting information.</param> /// <returns>A nullable DateTime equivalent to the value of this instance.</returns> public override DateTime?ToDateTime(IParameter targetParameter, IFormatProvider provider) { if (string.IsNullOrEmpty(_value)) { return(null); } DateTime result; if (!FixDateTime.TryParse(_value, provider, out result)) { throw ThrowHelper.New <InvalidCastException>(this, ErrorMessages.InvalidDateOrTimeValue, _value); } return(result); }