PutridParrot.Options/OptionExtensions.cs
/// <summary>
/// Casts the value within the Option 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="option"></param>
/// <returns></returns>
Casts the value within the Option to a supplied type. If
the type conversion is not possible an InvalidCastException
is thrown.
Casts the value within the Option to a supplied type . If the type conversion is not possible an InvalidCastException is thrown .
public static Option<TTo> Cast<TFrom, TTo>(this Option<TFrom> option)
            where TTo : class
        {
            switch (option)
            {
                case Some<TFrom> some:
                    var cast = some.Value as TTo;
                    if (cast == null)
                    {
                        throw new InvalidCastException($"Cannot cast {some.Value.GetType().Name} to {typeof(TTo).Name}");
                    }
                    return cast.ToOption();
                default:
                    return Option<TTo>.None;
            }          
        }
public static Option < TTo > Cast < TFrom , TTo > ( this Option < TFrom > option ) where TTo : class { switch ( option ) { case Some < TFrom > some : var cast = some . Value as TTo ; if ( cast == null ) { throw new InvalidCastException ( $" Cannot cast  { some . Value . GetType ( ) . Name }  to  { typeof ( TTo ) . Name } " ) ; } return cast . ToOption ( ) ; default : return Option < TTo > . None ; } }
Casts the value within the Option to a supplied type.
Casts the value within the Option to a supplied type .
None
Option<TFrom>
this
option
None
typeparam
None
typeparam
None