PutridParrot.Options/OptionalExtensions.cs
/// <summary>
/// Casts the value within the optional to a supplied type. If
/// the type conversion is not possible an InvalidCastException
/// is thrown.
/// </summary>
/// <typeparam name="TFrom"></typeparam>
/// <typeparam name="TTo"></typeparam>
/// <param name="optional"></param>
/// <returns></returns>
Casts the value within the optional to a supplied type. If
the type conversion is not possible an InvalidCastException
is thrown.
Casts the value within the optional to a supplied type . If the type conversion is not possible an InvalidCastException is thrown .
public static Optional<TTo> Cast<TFrom, TTo>(this Optional<TFrom> optional)
            where TTo : class
        {
            var cast = optional.Value as TTo;
            if (cast == null)
            {
                throw new InvalidCastException($"Cannot cast {optional.Value.GetType().Name} to {typeof(TTo).Name}");
            }
            return cast.ToOptional();
        }
public static Optional < TTo > Cast < TFrom , TTo > ( this Optional < TFrom > optional ) where TTo : class { var cast = optional . Value as TTo ; if ( cast == null ) { throw new InvalidCastException ( $" Cannot cast  { optional . Value . GetType ( ) . Name }  to  { typeof ( TTo ) . Name } " ) ; } return cast . ToOptional ( ) ; }
Casts the value within the optional to a supplied type.
Casts the value within the optional to a supplied type .
None
Optional<TFrom>
this
optional
None
typeparam
None
typeparam
None