/// <summary> /// A formatter implementation that resolves a <see cref="IConvertible"/>. /// </summary> /// <param name="hierarchy">The hierarchy to parse.</param> /// <returns>A <see cref="IConvertible"/> from the specified <paramref name="hierarchy"/>.</returns> public static IConvertible UseConvertibleFormatter(this IHierarchy <DataPair> hierarchy) { Validator.ThrowIfNull(hierarchy, nameof(hierarchy)); var i = hierarchy.FindSingleInstance(h => ConvertibleTypes.Select(pair => pair.Value).Contains(h.Instance.Name)); return(ObjectConverter.ChangeType(i.Value, ConvertibleTypes.Single(pair => pair.Value == i.Name).Key) as IConvertible); }
/// <summary> /// A formatter implementation that resolves a <see cref="Uri"/>. /// </summary> /// <param name="hierarchy">The hierarchy to parse.</param> /// <returns>A <see cref="Uri"/> from the specified <paramref name="hierarchy"/>.</returns> public static Uri UseUriFormatter(this IHierarchy <DataPair> hierarchy) { Validator.ThrowIfNull(hierarchy, nameof(hierarchy)); var uri = hierarchy.FindSingleInstance(h => h.Instance.Name.Equals("OriginalString", StringComparison.OrdinalIgnoreCase)); return(uri == null?hierarchy.UseGenericConverter <Uri>() : uri.Value.ToString().ToUri()); }
/// <summary> /// A formatter implementation that resolves a <see cref="TimeSpan"/>. /// </summary> /// <param name="hierarchy">The hierarchy to parse.</param> /// <returns>A <see cref="TimeSpan"/> from the specified <paramref name="hierarchy"/>.</returns> public static TimeSpan UseTimeSpanFormatter(this IHierarchy <DataPair> hierarchy) { Validator.ThrowIfNull(hierarchy, nameof(hierarchy)); var ticks = hierarchy.FindSingleInstance(h => h.Instance.Name.Equals("Ticks", StringComparison.OrdinalIgnoreCase)); return(ticks == null?hierarchy.UseGenericConverter <TimeSpan>() : TimeSpan.FromTicks(Convert.ToInt64(ticks.Value))); }
/// <summary> /// A formatter implementation that resolves a <see cref="DateTime"/>. /// </summary> /// <param name="hierarchy">The hierarchy to parse.</param> /// <returns>A <see cref="DateTime"/> from the specified <paramref name="hierarchy"/>.</returns> public static DateTime UseDateTimeFormatter(this IHierarchy <DataPair> hierarchy) { Validator.ThrowIfNull(hierarchy, nameof(hierarchy)); return(new DateTime(Convert.ToInt64(hierarchy.FindSingleInstance(h => h.Instance.Name.Equals("Ticks", StringComparison.OrdinalIgnoreCase)).Value))); }