Skip to content

Commit

Permalink
Documented IConvertable extension methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Teodor92 committed Mar 12, 2016
1 parent 5578c1e commit c011c18
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Source/MoreDotNet/Extentions/Common/ByteArrayExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class ByteArrayExtentions
/// Converts a byte array to a string, using its byte order mark to convert it to the right encoding.
/// Original article: http://www.west-wind.com/WebLog/posts/197245.aspx
/// </summary>
/// <param name="buffer">An array of bytes to convert</param>
/// <param name="buffer">An array of bytes to convert.</param>
/// <returns>The byte as a string.</returns>
public static string GetString(this byte[] buffer)
{
Expand All @@ -19,7 +19,7 @@ public static string GetString(this byte[] buffer)
}

// ANSI as default
Encoding encoding = Encoding.Default;
Encoding encoding = Encoding.Default;

/*
EF BB BF UTF-8
Expand Down
47 changes: 47 additions & 0 deletions Source/MoreDotNet/Extentions/Common/ConvertibleExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@

public static class ConvertibleExtentions
{
/// <summary>
/// Converts an <see cref="IConvertible"/> object to the <see cref="T"/> type.
/// </summary>
/// <typeparam name="T">Type to be converted to.</typeparam>
/// <param name="obj">Object to be converted.</param>
/// <returns>The converted object.</returns>
public static T To<T>(this IConvertible obj)
{
return (T)Convert.ChangeType(obj, typeof(T));
}

/// <summary>
/// Converts an <see cref="IConvertible"/> object to the <see cref="T"/> type or returns the default value of the <see cref="T"/> type.
/// </summary>
/// <typeparam name="T">Type to be converted to.</typeparam>
/// <param name="obj">Object to be converted.</param>
/// <returns>The converted object or its default value.</returns>
public static T ToOrDefault<T>(this IConvertible obj)
{
try
Expand All @@ -21,6 +33,13 @@ public static T ToOrDefault<T>(this IConvertible obj)
}
}

/// <summary>
/// Converts an <see cref="IConvertible"/> object to the <see cref="T"/> type and assigns it to the <paramref name="newObj"/> or assigns the default value of the <see cref="T"/> type.
/// </summary>
/// <typeparam name="T">Type to be converted to.</typeparam>
/// <param name="obj">Object to be converted.</param>
/// <param name="newObj">Object to which the result is assigned.</param>
/// <returns>True if conversion is successful or false otherwise.</returns>
public static bool ToOrDefault<T>(this IConvertible obj, out T newObj)
{
try
Expand All @@ -35,6 +54,13 @@ public static bool ToOrDefault<T>(this IConvertible obj, out T newObj)
}
}

/// <summary>
/// Converts an <see cref="IConvertible"/> object to the <see cref="T"/> type or returns the <paramref name="other"/> object.
/// </summary>
/// <typeparam name="T">Type to be converted to.</typeparam>
/// <param name="obj">Object to be converted.</param>
/// <param name="other">Object to be returned in case of failure.</param>
/// <returns>The converted object of type <see cref="T"/> or its alternative <paramref name="other"/></returns>
public static T ToOrOther<T>(this IConvertible obj, T other)
{
try
Expand All @@ -47,6 +73,14 @@ public static T ToOrOther<T>(this IConvertible obj, T other)
}
}

/// <summary>
/// Converts an <see cref="IConvertible"/> object to the <see cref="T"/> type and assigns it to the <paramref name="newObj"/>. If that is not possible assigns the <paramref name="other"/> to <paramref name="newObj"/>.
/// </summary>
/// <typeparam name="T">Type to be converted to.</typeparam>
/// <param name="obj">Object to be converted.</param>
/// <param name="newObj">Object to which the result is assigned.</param>
/// <param name="other">Object to be assigned in case of failure.</param>
/// <returns>True if conversion is successful or false otherwise.</returns>
public static bool ToOrOther<T>(this IConvertible obj, out T newObj, T other)
{
try
Expand All @@ -61,6 +95,12 @@ public static bool ToOrOther<T>(this IConvertible obj, out T newObj, T other)
}
}

/// <summary>
/// Converts an <see cref="IConvertible"/> object to the <see cref="T"/> type or returns null.
/// </summary>
/// <typeparam name="T">Type to be converted to.</typeparam>
/// <param name="obj">Object to be converted.</param>
/// <returns>The converted object or null.</returns>
public static T ToOrNull<T>(this IConvertible obj) where T : class
{
try
Expand All @@ -73,6 +113,13 @@ public static T ToOrNull<T>(this IConvertible obj) where T : class
}
}

/// <summary>
/// Converts an <see cref="IConvertible"/> object to the <see cref="T"/> type and assigns the result to <paramref name="newObj"/>. If that is not possible assigns null.
/// </summary>
/// <typeparam name="T">Type to be converted to.</typeparam>
/// <param name="obj">Object to be converted.</param>
/// <param name="newObj">Object to which the result is assigned.</param>
/// <returns>True if conversion is successful or false otherwise.</returns>
public static bool ToOrNull<T>(this IConvertible obj, out T newObj) where T : class
{
try
Expand Down

0 comments on commit c011c18

Please # to comment.