public void Identity_Identities() { var iconv = _converter.GetConverter <int, int>(); Assert.IsTrue(iconv(42).IsSuccessful, "IdentityConverter should convert an int to itself"); Assert.AreEqual(42, iconv(42).Result, "IdentityConverter should preserve the value of an int when converted to itself"); var sconv = _converter.GetConverter <string, string>(); Assert.IsTrue(sconv("hello").IsSuccessful, "IdentityConverter should convert a string to itself"); Assert.AreEqual("hello", sconv("hello").Result, "IdentityConverter should preserve the value of a string when converted to itself"); Assert.IsTrue(_converter.Convert(new Dummy()).TryTo(out Dummy result)); }
public static Action <WebProgressTask, object?>?ApplyResult(IDataConverter converter, Type type) { var conv = converter.GetConverter(type); if (conv is null) { return(null); } return((task, value) => { if (value != null) { var source = conv(value); if (source != null) { task.Document.DataSources.Add(source); } else { task.Document.DataSources.Add(new HttpStringDataSource("Cannot create data")); task.Response.StatusCode = HttpStateCode.InternalServerError; } } }); }
private static Measure ComplexPrimitiveNumeric(IDataConverter converter) { var c = converter.GetConverter <double, decimal>(); return(Measure.Create("complex primitive numeric conversion") .ReferenceAction(() => { var _ = Convert.ToDecimal(3.14159); }) .MeasurementAction(() => { var _ = c(3.14159).Result; })); }
private static Measure StringToBool(IDataConverter converter) { var c = converter.GetConverter <string, bool>(); return(Measure.Create("string to bool conversion") .ReferenceAction(() => { var _ = Convert.ToBoolean("TRUE"); }) .MeasurementAction(() => { var _ = c("TRUE").Result; })); }
private static Measure SimplePrimitiveNumeric(IDataConverter converter) { var c = converter.GetConverter <int, short>(); return(Measure.Create("simple primitive numeric conversion") .ReferenceAction(() => { var _ = Convert.ToInt16(42); }) .MeasurementAction(() => { var _ = c(42).Result; })); }
private static Measure IntToBool(IDataConverter converter) { var c = converter.GetConverter <int, bool>(); return(Measure.Create("int to bool conversion") .ReferenceAction(() => { var _ = Convert.ToBoolean(1); }) .MeasurementAction(() => { var _ = c(1).Result; })); }
private static Measure ParseDate(IDataConverter converter) { var c = converter.GetConverter <string, DateTime>(); return(Measure.Create("DateTime to string conversion") .ReferenceAction(() => { var _ = DateTime.TryParse("2017-04-20T08:51:00Z", null, DateTimeStyles.RoundtripKind, out var res); }) .MeasurementAction(() => { var _ = c("2017-04-20T08:51:00Z").Result; })); }
private static Measure DynamicPrimitiveNumeric(IDataConverter converter) { var c = converter.GetConverter <object, short>(); return(Measure.Create("dynamic primitive numeric conversion") .ReferenceAction(() => { var _ = Convert.ToInt16((object)42); }) .MeasurementAction(() => { var _ = c(42).Result; })); }
private static Measure DynamicStringToInt(IDataConverter converter) { var c = converter.GetConverter <object, int>(); var ic = CultureInfo.InvariantCulture; return(Measure.Create("dynamic string to int conversion") .ReferenceAction(() => { var _ = Convert.ToInt32((object)"42", ic); }) .MeasurementAction(() => { var _ = c("42").Result; })); }
private static Measure StringToNullableInt(IDataConverter converter) { var c = converter.GetConverter <string, int?>(); var ic = CultureInfo.InvariantCulture; return(Measure.Create("string to int? conversion") .ReferenceAction(() => { var _ = (int?)Convert.ToInt32("42"); }) .MeasurementAction(() => { var _ = c("42").Result; })); }
private static Measure IntToString(IDataConverter converter) { var c = converter.GetConverter <int, string>(); var ic = CultureInfo.InvariantCulture; return(Measure.Create("int to string conversion") .ReferenceAction(() => { var _ = 42.ToString(ic); }) .MeasurementAction(() => { var _ = c(42).Result; })); }
public ListConverter(GenericVector sexp, IDataConverter converter) { var array = sexp.ToArray(); _length = sexp.Length; _intersectedItemType = null; _converters = new IConverter[_length]; for (var i = 0; i < _length; i++) { _converters[i] = converter.GetConverter(array[i].DangerousGetHandle().ToInt64()); if (_converters[i] == null) { throw new InvalidDataException("Unable to get convert for data at index: " + i + " in List"); } var itemTypes = _converters[i].GetClrTypes(); _intersectedItemType = _intersectedItemType == null ? itemTypes : _intersectedItemType.Intersect(itemTypes); } if (_intersectedItemType == null) { _intersectedItemType = new[] { typeof(object) } } ; var fullTypes = new List <Type>(); _names = sexp.Names; if (_names != null) { fullTypes.AddRange(keyTypes.GetDictionaryTypes(_intersectedItemType)); if (_names.Length != _length) { var swap = new string[_length]; for (var i = 0; i < _length; i++) { swap[i] = i < _names.Length ? _names[i] : "Column " + (i + 1); } _names = swap; } } fullTypes.AddRange(_intersectedItemType.GetListOrArrayTypes()); var count = fullTypes.Count; _types = fullTypes[0].GetFullHierarchy(); for (var i = 1; i < count; i++) { _types = _types.Union(fullTypes[i].GetFullHierarchy()); } }
/// <summary> /// This method performs an actual conversion from a value of type T to type U. /// </summary> /// <typeparam name="T">The type to be converted from.</typeparam> /// <typeparam name="U">The type to be converted to.</typeparam> /// <param name="converter">The IDataConverter object.</param> /// <param name="value">The value to be converted.</param> /// <returns>The result of the conversion of the value to type U.</returns> public static ConversionResult <U> DoConversion <T, U>(this IDataConverter converter, T value) => converter.GetConverter <T, U>()(value);
/// <summary> /// Gets a strongly typed converter function /// </summary> /// <typeparam name="T">The type to be converted from.</typeparam> /// <typeparam name="U">The type to be converted to.</typeparam> /// <param name="converter">The IDataConverter object that provides the non-generic converter.</param> /// <returns>A strongly typed converter function.</returns> public static Func <T, ConversionResult <U> > GetConverter <T, U>(this IDataConverter converter) => (Func <T, ConversionResult <U> >)converter.GetConverter(typeof(T), typeof(U));