Guest User

Function private and static locals in C#

a guest
Jun 13th, 2011
258
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /// <summary>
  2.     ///   Class which serves as a workaround to support variables scoped even beyond private.
  3.     /// </summary>
  4.     /// <typeparam name="TValue">The type of the local value.</typeparam>
  5.     /// <author>Steven Jeuris</author>
  6.     public class Private<TValue>
  7.     {
  8.         static readonly Dictionary<object, object> StaticScope = new Dictionary<object, object>();
  9.         static readonly Dictionary<object, Dictionary<object, object>> InstanceScope = new Dictionary<object, Dictionary<object, object>>();
  10.  
  11.  
  12.         public TValue Value { get; set; }
  13.  
  14.  
  15.         private Private( TValue value )
  16.         {
  17.             Value = value;
  18.         }
  19.  
  20.  
  21.         /// <summary>
  22.         ///   Create a new static local value which can only be accessed from the scope it was created in.
  23.         ///   The value is shared among all instances, like an ordinary static.
  24.         /// </summary>
  25.         /// <param name="initialValue">Initializer for the value.</param>
  26.         /// <returns>An instance of <see cref="Private{T}" /> through which the value can be accessed.</returns>
  27.         public static Private<TValue> Static( Func<TValue> initialValue )
  28.         {
  29.             if ( !StaticScope.ContainsKey( initialValue ) )
  30.             {
  31.                 Private<TValue> newInstance = new Private<TValue>( initialValue() );
  32.                 StaticScope.Add( initialValue, newInstance );
  33.             }
  34.  
  35.             return StaticScope[ initialValue ] as Private<TValue>;
  36.         }
  37.  
  38.         /// <summary>
  39.         ///   Create a new local value bound to a single instance, but only accessible from within the scope it was created in.
  40.         /// </summary>
  41.         /// <typeparam name="TScope">The type of the class of the instance scope.</typeparam>
  42.         /// <param name="initialValue">Initializer for the value.</param>
  43.         /// <param name="instance">The instance to which the value is bound.</param>
  44.         /// <returns>An instance of <see cref="Private{T}" /> through which the value can be accessed.</returns>
  45.         public static Private<TValue> Instance<TScope>( Func<TValue> initialValue, TScope instance )
  46.         {
  47.             if ( !InstanceScope.ContainsKey( instance ) )
  48.             {
  49.                 InstanceScope.Add( instance, new Dictionary<object, object>() );
  50.  
  51.                 if ( !InstanceScope[ instance ].ContainsKey( instance ) )
  52.                 {
  53.                     Private<TValue> newInstance = new Private<TValue>( initialValue() );
  54.                     InstanceScope[ instance ].Add( initialValue, newInstance );
  55.                 }
  56.             }
  57.  
  58.             return InstanceScope[ instance ][ initialValue ] as Private<TValue>;
  59.         }
  60.     }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×