Advertisement
Guest User

Untitled

a guest
Apr 5th, 2014
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.97 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace RT.Util
  5. {
  6.     /// <summary>Provides a function delegate that accepts only value types as input and return types.</summary>
  7.     public delegate TResult FuncStructStruct<in TInput, TResult>(TInput input)
  8.         where TInput : struct
  9.         where TResult : struct;
  10.     /// <summary>Provides a function delegate that accepts only value types as input and only reference types as return types.</summary>
  11.     public delegate TResult FuncStructClass<in TInput, TResult>(TInput input)
  12.         where TInput : struct
  13.         where TResult : class;
  14.     /// <summary>Provides a function delegate that accepts only reference types as input and only value types as return types.</summary>
  15.     public delegate TResult FuncClassStruct<in TInput, TResult>(TInput input)
  16.         where TInput : class
  17.         where TResult : struct;
  18.     /// <summary>Provides a function delegate that accepts only reference types as input and return types.</summary>
  19.     public delegate TResult FuncClassClass<in TInput, TResult>(TInput input)
  20.         where TInput : class
  21.         where TResult : class;
  22.     /// <summary>Provides a function delegate that accepts only reference types as input.</summary>
  23.     public delegate TResult FuncClass<in TInput, TResult>(TInput input) where TInput : class;
  24.     /// <summary>Provides a function delegate that accepts only value types as input.</summary>
  25.     public delegate TResult FuncStruct<in TInput, TResult>(TInput input) where TInput : struct;
  26.  
  27.     /// <summary>Provides extension methods that apply to all types.</summary>
  28.     public static class Ut
  29.     {
  30.         /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
  31.         /// <typeparam name="TInput">Type of the input value.</typeparam>
  32.         /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
  33.         /// <param name="input">Input value to check for null.</param>
  34.         /// <param name="lambda">Function to apply the input value to if it is not null.</param>
  35.         public static TResult NullOr<TInput, TResult>(this TInput input, FuncClassClass<TInput, TResult> lambda)
  36.             where TInput : class
  37.             where TResult : class
  38.         {
  39.             return input == null ? null : lambda(input);
  40.         }
  41.  
  42.         /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
  43.         /// <typeparam name="TInput">Type of the input value.</typeparam>
  44.         /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
  45.         /// <param name="input">Input value to check for null.</param>
  46.         /// <param name="lambda">Function to apply the input value to if it is not null.</param>
  47.         public static TResult? NullOr<TInput, TResult>(this TInput input, FuncClass<TInput, TResult?> lambda)
  48.             where TInput : class
  49.             where TResult : struct
  50.         {
  51.             return input == null ? null : lambda(input);
  52.         }
  53.  
  54.         /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
  55.         /// <typeparam name="TInput">Type of the input value.</typeparam>
  56.         /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
  57.         /// <param name="input">Input value to check for null.</param>
  58.         /// <param name="lambda">Function to apply the input value to if it is not null.</param>
  59.         public static TResult? NullOr<TInput, TResult>(this TInput input, FuncClassStruct<TInput, TResult> lambda)
  60.             where TInput : class
  61.             where TResult : struct
  62.         {
  63.             return input == null ? null : (TResult?) lambda(input);
  64.         }
  65.  
  66.         /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
  67.         /// <typeparam name="TInput">Type of the input value.</typeparam>
  68.         /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
  69.         /// <param name="input">Input value to check for null.</param>
  70.         /// <param name="lambda">Function to apply the input value to if it is not null.</param>
  71.         public static TResult NullOr<TInput, TResult>(this TInput? input, FuncStructClass<TInput, TResult> lambda)
  72.             where TInput : struct
  73.             where TResult : class
  74.         {
  75.             return input == null ? null : lambda(input.Value);
  76.         }
  77.  
  78.         /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
  79.         /// <typeparam name="TInput">Type of the input value.</typeparam>
  80.         /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
  81.         /// <param name="input">Input value to check for null.</param>
  82.         /// <param name="lambda">Function to apply the input value to if it is not null.</param>
  83.         public static TResult? NullOr<TInput, TResult>(this TInput? input, FuncStruct<TInput, TResult?> lambda)
  84.             where TInput : struct
  85.             where TResult : struct
  86.         {
  87.             return input == null ? null : lambda(input.Value);
  88.         }
  89.  
  90.         /// <summary>Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.</summary>
  91.         /// <typeparam name="TInput">Type of the input value.</typeparam>
  92.         /// <typeparam name="TResult">Type of the result from the lambda.</typeparam>
  93.         /// <param name="input">Input value to check for null.</param>
  94.         /// <param name="lambda">Function to apply the input value to if it is not null.</param>
  95.         public static TResult? NullOr<TInput, TResult>(this TInput? input, FuncStructStruct<TInput, TResult> lambda)
  96.             where TInput : struct
  97.             where TResult : struct
  98.         {
  99.             return input == null ? null : (TResult?) lambda(input.Value);
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement