Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace RT.Util
- {
- /// <summary>Provides a function delegate that accepts only value types as input and return types.</summary>
- public delegate TResult FuncStructStruct<in TInput, TResult>(TInput input)
- where TInput : struct
- where TResult : struct;
- /// <summary>Provides a function delegate that accepts only value types as input and only reference types as return types.</summary>
- public delegate TResult FuncStructClass<in TInput, TResult>(TInput input)
- where TInput : struct
- where TResult : class;
- /// <summary>Provides a function delegate that accepts only reference types as input and only value types as return types.</summary>
- public delegate TResult FuncClassStruct<in TInput, TResult>(TInput input)
- where TInput : class
- where TResult : struct;
- /// <summary>Provides a function delegate that accepts only reference types as input and return types.</summary>
- public delegate TResult FuncClassClass<in TInput, TResult>(TInput input)
- where TInput : class
- where TResult : class;
- /// <summary>Provides a function delegate that accepts only reference types as input.</summary>
- public delegate TResult FuncClass<in TInput, TResult>(TInput input) where TInput : class;
- /// <summary>Provides a function delegate that accepts only value types as input.</summary>
- public delegate TResult FuncStruct<in TInput, TResult>(TInput input) where TInput : struct;
- /// <summary>Provides extension methods that apply to all types.</summary>
- public static class Ut
- {
- /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
- /// <typeparam name="TInput">Type of the input value.</typeparam>
- /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
- /// <param name="input">Input value to check for null.</param>
- /// <param name="lambda">Function to apply the input value to if it is not null.</param>
- public static TResult NullOr<TInput, TResult>(this TInput input, FuncClassClass<TInput, TResult> lambda)
- where TInput : class
- where TResult : class
- {
- return input == null ? null : lambda(input);
- }
- /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
- /// <typeparam name="TInput">Type of the input value.</typeparam>
- /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
- /// <param name="input">Input value to check for null.</param>
- /// <param name="lambda">Function to apply the input value to if it is not null.</param>
- public static TResult? NullOr<TInput, TResult>(this TInput input, FuncClass<TInput, TResult?> lambda)
- where TInput : class
- where TResult : struct
- {
- return input == null ? null : lambda(input);
- }
- /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
- /// <typeparam name="TInput">Type of the input value.</typeparam>
- /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
- /// <param name="input">Input value to check for null.</param>
- /// <param name="lambda">Function to apply the input value to if it is not null.</param>
- public static TResult? NullOr<TInput, TResult>(this TInput input, FuncClassStruct<TInput, TResult> lambda)
- where TInput : class
- where TResult : struct
- {
- return input == null ? null : (TResult?) lambda(input);
- }
- /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
- /// <typeparam name="TInput">Type of the input value.</typeparam>
- /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
- /// <param name="input">Input value to check for null.</param>
- /// <param name="lambda">Function to apply the input value to if it is not null.</param>
- public static TResult NullOr<TInput, TResult>(this TInput? input, FuncStructClass<TInput, TResult> lambda)
- where TInput : struct
- where TResult : class
- {
- return input == null ? null : lambda(input.Value);
- }
- /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
- /// <typeparam name="TInput">Type of the input value.</typeparam>
- /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
- /// <param name="input">Input value to check for null.</param>
- /// <param name="lambda">Function to apply the input value to if it is not null.</param>
- public static TResult? NullOr<TInput, TResult>(this TInput? input, FuncStruct<TInput, TResult?> lambda)
- where TInput : struct
- where TResult : struct
- {
- return input == null ? null : lambda(input.Value);
- }
- /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
- /// <typeparam name="TInput">Type of the input value.</typeparam>
- /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
- /// <param name="input">Input value to check for null.</param>
- /// <param name="lambda">Function to apply the input value to if it is not null.</param>
- public static TResult? NullOr<TInput, TResult>(this TInput? input, FuncStructStruct<TInput, TResult> lambda)
- where TInput : struct
- where TResult : struct
- {
- return input == null ? null : (TResult?) lambda(input.Value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement