Skip to content

Commit

Permalink
Fixed #6: Added specialised ToX methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ygoe committed Aug 21, 2019
1 parent 15f6392 commit 37ddd9e
Showing 1 changed file with 346 additions and 4 deletions.
350 changes: 346 additions & 4 deletions DeepConvert/DeepConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace Unclassified.Util
/// </summary>
public static class DeepConvert
{
#region Private data

/// <summary>
/// All words that are considered boolean false. Anything else is considered true.
/// </summary>
Expand Down Expand Up @@ -63,6 +65,10 @@ public static class DeepConvert
/// </summary>
private static DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

#endregion Private data

#region Main ChangeType methods

/// <summary>
/// Returns an object of the specified type whose value is equivalent to the specified
/// object.
Expand Down Expand Up @@ -569,20 +575,24 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
throw new InvalidCastException($"The value '{value}' ({srcType.FullName}) cannot be converted to {destType.FullName}.");
}

#endregion Main ChangeType methods

#region ToDateTime methods

/// <summary>
/// Returns a DateTime value whose value is equivalent to the specified object.
/// Returns a <see cref="DateTime"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A DateTime value whose value is equivalent to <paramref name="value"/>.</returns>
/// <returns>A <see cref="DateTime"/> value that is equivalent to <paramref name="value"/>.</returns>
public static DateTime ToDateTime(object value) =>
ToDateTime(value, new DeepConvertSettings());

/// <summary>
/// Returns a DateTime value whose value is equivalent to the specified object.
/// Returns a <see cref="DateTime"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A DateTime value whose value is equivalent to <paramref name="value"/>.</returns>
/// <returns>A <see cref="DateTime"/> value that is equivalent to <paramref name="value"/>.</returns>
public static DateTime ToDateTime(object value, DeepConvertSettings settings)
{
var provider = settings.Provider;
Expand Down Expand Up @@ -661,6 +671,336 @@ public static DateTime ToDateTime(object value, DeepConvertSettings settings)
throw new InvalidCastException($"The value '{value}' ({srcType.FullName}) cannot be converted to DateTime.");
}

#endregion ToDateTime methods

#region Simple ToX methods

#region int8

/// <summary>
/// Returns a <see cref="byte"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="byte"/> value that is equivalent to <paramref name="value"/>.</returns>
public static byte ToByte(object value) =>
ChangeType<byte>(value);

/// <summary>
/// Returns a <see cref="byte"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="byte"/> value that is equivalent to <paramref name="value"/>.</returns>
public static byte ToByte(object value, DeepConvertSettings settings) =>
ChangeType<byte>(value, settings);

/// <summary>
/// Returns a <see cref="sbyte"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="sbyte"/> value that is equivalent to <paramref name="value"/>.</returns>
public static sbyte ToSByte(object value) =>
ChangeType<sbyte>(value);

/// <summary>
/// Returns a <see cref="sbyte"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="sbyte"/> value that is equivalent to <paramref name="value"/>.</returns>
public static sbyte ToSByte(object value, DeepConvertSettings settings) =>
ChangeType<sbyte>(value, settings);

#endregion int8

#region int16

/// <summary>
/// Returns a <see cref="ushort"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="ushort"/> value that is equivalent to <paramref name="value"/>.</returns>
public static ushort ToUInt16(object value) =>
ChangeType<ushort>(value);

/// <summary>
/// Returns a <see cref="ushort"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="ushort"/> value that is equivalent to <paramref name="value"/>.</returns>
public static ushort ToUInt16(object value, DeepConvertSettings settings) =>
ChangeType<ushort>(value, settings);

/// <summary>
/// Returns a <see cref="short"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="short"/> value that is equivalent to <paramref name="value"/>.</returns>
public static short ToInt16(object value) =>
ChangeType<short>(value);

/// <summary>
/// Returns a <see cref="short"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="short"/> value that is equivalent to <paramref name="value"/>.</returns>
public static short ToInt16(object value, DeepConvertSettings settings) =>
ChangeType<short>(value, settings);

#endregion int16

#region int32

/// <summary>
/// Returns a <see cref="uint"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="uint"/> value that is equivalent to <paramref name="value"/>.</returns>
public static uint ToUInt32(object value) =>
ChangeType<uint>(value);

/// <summary>
/// Returns a <see cref="uint"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="uint"/> value that is equivalent to <paramref name="value"/>.</returns>
public static uint ToUInt32(object value, DeepConvertSettings settings) =>
ChangeType<uint>(value, settings);

/// <summary>
/// Returns a <see cref="int"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="int"/> value that is equivalent to <paramref name="value"/>.</returns>
public static int ToInt32(object value) =>
ChangeType<int>(value);

/// <summary>
/// Returns a <see cref="int"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="int"/> value that is equivalent to <paramref name="value"/>.</returns>
public static int ToInt32(object value, DeepConvertSettings settings) =>
ChangeType<int>(value, settings);

#endregion int32

#region int64

/// <summary>
/// Returns a <see cref="ulong"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="ulong"/> value that is equivalent to <paramref name="value"/>.</returns>
public static ulong ToUInt64(object value) =>
ChangeType<ulong>(value);

/// <summary>
/// Returns a <see cref="ulong"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="ulong"/> value that is equivalent to <paramref name="value"/>.</returns>
public static ulong ToUInt64(object value, DeepConvertSettings settings) =>
ChangeType<ulong>(value, settings);

/// <summary>
/// Returns a <see cref="long"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="long"/> value that is equivalent to <paramref name="value"/>.</returns>
public static long ToInt64(object value) =>
ChangeType<long>(value);

/// <summary>
/// Returns a <see cref="long"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="long"/> value that is equivalent to <paramref name="value"/>.</returns>
public static long ToInt64(object value, DeepConvertSettings settings) =>
ChangeType<long>(value, settings);

#endregion int16

#region decimal

/// <summary>
/// Returns a <see cref="decimal"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="decimal"/> value that is equivalent to <paramref name="value"/>.</returns>
public static decimal ToDecimal(object value) =>
ChangeType<decimal>(value);

/// <summary>
/// Returns a <see cref="decimal"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="decimal"/> value that is equivalent to <paramref name="value"/>.</returns>
public static decimal ToDecimal(object value, DeepConvertSettings settings) =>
ChangeType<decimal>(value, settings);

#endregion decimal

#region floats

/// <summary>
/// Returns a <see cref="float"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="float"/> value that is equivalent to <paramref name="value"/>.</returns>
public static float ToSingle(object value) =>
ChangeType<float>(value);

/// <summary>
/// Returns a <see cref="float"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="float"/> value that is equivalent to <paramref name="value"/>.</returns>
public static float ToSingle(object value, DeepConvertSettings settings) =>
ChangeType<float>(value, settings);

/// <summary>
/// Returns a <see cref="double"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="double"/> value that is equivalent to <paramref name="value"/>.</returns>
public static double ToDouble(object value) =>
ChangeType<double>(value);

/// <summary>
/// Returns a <see cref="double"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="double"/> value that is equivalent to <paramref name="value"/>.</returns>
public static double ToDouble(object value, DeepConvertSettings settings) =>
ChangeType<double>(value, settings);

#endregion floats

#region bool

/// <summary>
/// Returns a <see cref="bool"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="bool"/> value that is equivalent to <paramref name="value"/>.</returns>
public static bool ToBoolean(object value) =>
ChangeType<bool>(value);

/// <summary>
/// Returns a <see cref="bool"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="bool"/> value that is equivalent to <paramref name="value"/>.</returns>
public static bool ToBoolean(object value, DeepConvertSettings settings) =>
ChangeType<bool>(value, settings);

#endregion bool

#region string

/// <summary>
/// Returns a <see cref="string"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A <see cref="string"/> value that is equivalent to <paramref name="value"/>.</returns>
public static string ToString(object value) =>
ChangeType<string>(value);

/// <summary>
/// Returns a <see cref="string"/> value that is equivalent to the specified object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A <see cref="string"/> value that is equivalent to <paramref name="value"/>.</returns>
public static string ToString(object value, DeepConvertSettings settings) =>
ChangeType<string>(value, settings);

#endregion string

#region array

/// <summary>
/// Returns an array of the specified type whose value is equivalent to the specified
/// object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>An array whose value is equivalent to <paramref name="value"/>.</returns>
public static T[] ToArray<T>(object value) =>
ChangeType<T[]>(value);

/// <summary>
/// Returns an array of the specified type whose value is equivalent to the specified
/// object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>An array whose value is equivalent to <paramref name="value"/>.</returns>
public static T[] ToArray<T>(object value, DeepConvertSettings settings) =>
ChangeType<T[]>(value, settings);

#endregion array

#region list

/// <summary>
/// Returns a list of the specified type whose value is equivalent to the specified
/// object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A list whose value is equivalent to <paramref name="value"/>.</returns>
public static List<T> ToList<T>(object value) =>
ChangeType<List<T>>(value);

/// <summary>
/// Returns a list of the specified type whose value is equivalent to the specified
/// object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A list whose value is equivalent to <paramref name="value"/>.</returns>
public static List<T> ToList<T>(object value, DeepConvertSettings settings) =>
ChangeType<List<T>>(value, settings);

#endregion list

#region dictionary

/// <summary>
/// Returns a dictionary of the specified type whose value is equivalent to the specified
/// object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <returns>A dictionary whose value is equivalent to <paramref name="value"/>.</returns>
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(object value) =>
ChangeType<Dictionary<TKey, TValue>>(value);

/// <summary>
/// Returns a dictionary of the specified type whose value is equivalent to the specified
/// object.
/// </summary>
/// <param name="value">The data to convert.</param>
/// <param name="settings">The conversion settings.</param>
/// <returns>A dictionary whose value is equivalent to <paramref name="value"/>.</returns>
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(object value, DeepConvertSettings settings) =>
ChangeType<Dictionary<TKey, TValue>>(value, settings);

#endregion dictionary

#endregion Simple ToX methods

#region Helper methods

/// <summary>
/// Returns the default value of the specified type.
/// </summary>
Expand Down Expand Up @@ -736,6 +1076,8 @@ private static DateTime ConvertNumeric(double num, DateNumericKind numericKind,
throw new ArgumentException("Unknown date numeric kind.");
}
}

#endregion Helper methods
}

/// <summary>
Expand Down

0 comments on commit 37ddd9e

Please sign in to comment.