Пример #1
0
        /// <summary>
        /// Gets the <see cref="Maybe{TResult}"/> of the projection function <paramref name="func"/>.
        /// </summary>
        /// <typeparam name="TResult">The type of the projected result.</typeparam>
        /// <param name="func">The projection function.</param>
        /// <returns>The projected maybe.</returns>
        public Maybe <TResult> Some <TResult>(Func <T, TResult> func)
            where TResult : class
        {
            MaybeUtils.CheckArgumentNotNull(func, nameof(func));

            var lMaybe = Maybe.None <TResult>();

            this.Do(x => lMaybe = Maybe.Some(func(x)));

            return(lMaybe);
        }
Пример #2
0
        /// <summary>
        /// Gets the <see cref="MaybeStruct{TResult}"/> of the projection function <paramref name="func"/>.
        /// </summary>
        /// <typeparam name="TResult">The type of the projected result.</typeparam>
        /// <param name="func">The projection function.</param>
        /// <returns>The projected maybe.</returns>
        public MaybeStruct <TResult> SomeStruct <TResult>(Func <T, MaybeStruct <TResult> > func)
            where TResult : struct
        {
            MaybeUtils.CheckArgumentNotNull(func, nameof(func));

            var lMaybe = Maybe.NoneStruct <TResult>();

            this.Do(x => lMaybe = func(x));

            return(lMaybe);
        }
Пример #3
0
        /// <summary>
        /// Executes <paramref name="doAction"/> if value is not null.
        /// </summary>
        /// <param name="doAction">The action which is performed if maybe is not none.</param>
        /// <returns>The none case.</returns>
        public NoneCase Do(Action <T> doAction)
        {
            MaybeUtils.CheckArgumentNotNull(doAction, nameof(doAction));

            if (this.value != null)
            {
                doAction(this.value);
                return(new NoneCase(false));
            }

            return(new NoneCase(true));
        }
Пример #4
0
        /// <summary>
        /// Returns the value in case it is initialized.
        /// Otherwise throws an <see cref="InvalidOperationException"/>.
        /// </summary>
        /// <param name="maybeParameterName">The maybe reference used in error message.</param>
        /// <param name="additionalMessage">Additional error message.</param>
        /// <returns>The value.</returns>
        public T ValueOrException(
            string maybeParameterName = "",
            string additionalMessage  = "")
        {
            var parameterName = maybeParameterName;

            if (string.IsNullOrEmpty(maybeParameterName))
            {
                parameterName = typeof(T).Name;
            }

            return(MaybeUtils.CheckParameterNotNull(this.value, parameterName, additionalMessage));
        }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Maybe{T}"/> struct.
 /// Constructor used for deserialization.
 /// </summary>
 /// <param name="info">The serialization info.</param>
 /// <param name="context">The streaming context.</param>
 internal Maybe(SerializationInfo info, StreamingContext context)
 {
     this.value = MaybeUtils.GetDeserializedValue <T>(info, nameof(this.value));
 }
Пример #6
0
 /// <inheritdoc/>
 public void GetObjectData(SerializationInfo info, StreamingContext context)
 {
     MaybeUtils.SetData(info, nameof(this.value), this.value);
 }
Пример #7
0
 /// <summary>
 /// Returns the value in case it is initialized.
 /// Otherwise throws an <see cref="InvalidOperationException"/>.
 /// </summary>
 /// <param name="maybeParameterName">The maybe reference used in error message.</param>
 /// <param name="additionalMessage">Additional error message.</param>
 /// <returns>The value.</returns>
 public T ValueOrException(
     string maybeParameterName,
     string additionalMessage = "")
 {
     return(MaybeUtils.CheckParameterNotNull(this.value, maybeParameterName, additionalMessage));
 }
Пример #8
0
        /// <summary>
        /// Sets <paramref name="data"/> to serialization info.
        /// </summary>
        /// <typeparam name="T">The data's type.</typeparam>
        /// <param name="info">The serialization info.</param>
        /// <param name="key">The data's key.</param>
        /// <param name="data">The data.</param>
        internal static void SetData <T>(SerializationInfo info, string key, T data)
        {
            MaybeUtils.CheckArgumentNotNull(info, nameof(info));

            info.AddValue(key, data, typeof(T));
        }