Conversion helper with support for enumerations and Nullable I recently wrote this code as a more versatile stand-in for Convert.ChangeType. I have a nagging feeling that there's something I might be overlooking, or that there might be a more efficient algorithm for this.

    /// &lt;summary&gt;
    ///     Returns an object of type &lt;typeparamref name="T"/&gt; whose value is equivalent to that of the specified 
    ///     object.
    /// &lt;/summary&gt;
    /// &lt;typeparam name="T"&gt;
    ///     The output type.
    /// &lt;/typeparam&gt;
    /// &lt;param name="value"&gt;
    ///     An object that implements &lt;see cref="IConvertible"/&gt; or is &lt;see cref="Nullable{T}"/&gt; where the underlying
    ///     type implements &lt;see cref="IConvertible"/&gt;.
    /// &lt;/param&gt;
    /// &lt;returns&gt;
    ///     An object whose type is &lt;typeparamref name="T"/&gt; and whose value is equivalent to &lt;paramref name="value"/&gt;.
    /// &lt;/returns&gt;
    /// &lt;exception cref="System.ArgumentException"&gt;
    ///     The specified value is not defined by the enumeration (when &lt;typeparamref name="T"/&gt; is an enum, or Nullable{T}
    ///     where the underlying type is an enum).
    /// &lt;/exception&gt;
    /// &lt;exception cref="System.InvalidCastException"
    /// &lt;remarks&gt;
    ///     This method works similarly to &lt;see cref="Convert.ChangeType(object, Type)"/&gt; with the addition of support
    ///     for enumerations and &lt;see cref="Nullable{T}"/&gt; where the underlying type is &lt;see cref="IConvertible"/&gt;.
    /// &lt;/remarks&gt;
    internal static T ChangeType&lt;T&gt;(object value) {

        Type type = typeof(T);
        Type underlyingNullableType = Nullable.GetUnderlyingType(type);

        if ((underlyingNullableType ?? type).IsEnum) {

            // The specified type is an enum or Nullable{T} where T is an enum.

            T convertedEnum = (T)Enum.ToObject(underlyingNullableType ?? type, value);

            if (!Enum.IsDefined(underlyingNullableType ?? type, convertedEnum)) {
                throw new ArgumentException("The specified value is not defined by the enumeration.", "value");
            }

            return convertedEnum;
        } else if (type.IsValueType &amp;&amp; underlyingNullableType == null) {

            // The specified type is a non-nullable value type.

            if (value == null || DBNull.Value.Equals(value)) {
                throw new InvalidCastException("Cannot convert a null value to a non-nullable type.");
            }

            return (T)Convert.ChangeType(value, type);
        }

        // The specified type is a reference type or Nullable{T} where T is not an enum.
        return (value == null || DBNull.Value.Equals(value)) ? default(T) : (T)Convert.ChangeType(value, underlyingNullableType ?? type);
    }
/// &lt;summary&gt;
///     Returns an object of type &lt;typeparamref name="T"/&gt; whose value is equivalent to that of the specified 
///     object.
/// &lt;/summary&gt;

...

/// &lt;returns&gt;
///     An object whose type is &lt;typeparamref name="T"/&gt; and whose value is equivalent to &lt;paramref name="value"/&gt;.
/// &lt;/returns&gt;


I don't see any reason to repeat what was said in &lt;summary&gt; in &lt;returns&gt;. I wouldn't write &lt;returns&gt; here at all. (Unless you're writing a library with very high demands on documentation, like the .Net framework itself.)



underlyingNullableType ?? type


You're repeating this expression several times. You should probably extract it into a variable.



You should be consistent: either use if-else if-else and ignore the returns, or acknowledge the returns and use if-if-nothing.