Advertisement
Guest User

Propert<T> wrapper for C# property backing fields.

a guest
Feb 15th, 2011
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 KB | None | 0 0
  1. using System.Diagnostics.Contracts;
  2.  
  3.  
  4. namespace System.ComponentModel.Property
  5. {
  6.     /// <summary>
  7.     ///   Class which can be used as a backing field for a property.
  8.     ///   It supports common desired behavior when getting or setting a value.
  9.     /// </summary>
  10.     /// <typeparam name = "T">The type of the backing field for the property.</typeparam>
  11.     /// <author>Steven Jeuris</author>
  12.     public class Property<T>
  13.     {
  14.         public delegate void PropertyChanged( T oldValue, T newValue );
  15.  
  16.  
  17.         /// <summary>
  18.         ///   The backing field value for the property.
  19.         /// </summary>
  20.         T _value;
  21.  
  22.  
  23.         /// <summary>
  24.         ///   Create a new backing field for a property. Default value is the same as an ordinary field.
  25.         /// </summary>
  26.         public Property() {}
  27.  
  28.         /// <summary>
  29.         ///   Create a new backing field for a property with a specified default value.
  30.         /// </summary>
  31.         /// <param name = "defaultValue">The default value to be used.</param>
  32.         public Property( T defaultValue )
  33.         {
  34.             _value = defaultValue;
  35.         }
  36.  
  37.  
  38.         /// <summary>
  39.         ///   Get the current value of the backing field.
  40.         /// </summary>
  41.         /// <returns>The current value of the backing field.</returns>
  42.         public T GetValue()
  43.         {
  44.             return _value;
  45.         }
  46.  
  47.         /// <summary>
  48.         ///   Set a new value for the backing field.
  49.         /// </summary>
  50.         /// <param name = "newValue">The new value for the backing field.</param>
  51.         public void SetValue( T newValue )
  52.         {
  53.             SetValue( newValue, null );
  54.         }
  55.  
  56.         /// <summary>
  57.         ///   Set a new value for the backing field. When the new value is different from the current value, a callback is called.
  58.         /// </summary>
  59.         /// <param name = "newValue">The new value for the backing field.</param>
  60.         /// <param name = "changedCallback">The callback to call when the new value is different from the old one.</param>
  61.         public void SetValue( T newValue, PropertyChanged changedCallback )
  62.         {
  63.             SetValue( newValue, SetterCallbackOption.OnNewValue, changedCallback );
  64.         }
  65.  
  66.         bool _firstSetterCall = true;
  67.  
  68.         /// <summary>
  69.         ///   Set a new value for the backing field. The callback options specify when the callback should be called.
  70.         /// </summary>
  71.         /// <param name = "newValue">The new value for the backing field.</param>
  72.         /// <param name = "option">Option which specify when the callback should be called.</param>
  73.         /// <param name = "changedCallback">The callback which is called, based on the callback options.</param>
  74.         public void SetValue( T newValue, SetterCallbackOption option, PropertyChanged changedCallback )
  75.         {
  76.             Contract.Requires( Enum.IsDefined( typeof( SetterCallbackOption ), option ) );
  77.  
  78.             // Set new value.
  79.             T oldValue = _value;
  80.             _value = newValue;
  81.             bool isNewValue = !Equals( oldValue, newValue );
  82.  
  83.             // Trigger callback when necessary.
  84.             if ( changedCallback != null )
  85.             {
  86.                 bool shouldTriggerCallback = false;
  87.                 switch ( option )
  88.                 {
  89.                     case SetterCallbackOption.Always:
  90.                         shouldTriggerCallback = true;
  91.                         break;
  92.                     case SetterCallbackOption.OnNewValue:
  93.                         shouldTriggerCallback = isNewValue;
  94.                         break;
  95.                     case SetterCallbackOption.OnNewValueAndFirst:
  96.                         shouldTriggerCallback = isNewValue || _firstSetterCall;
  97.                         break;
  98.                     case SetterCallbackOption.OnSameValue:
  99.                         shouldTriggerCallback = Equals( oldValue, newValue );
  100.                         break;
  101.                 }
  102.  
  103.                 if ( shouldTriggerCallback )
  104.                 {
  105.                     changedCallback( oldValue, newValue );
  106.                 }
  107.             }
  108.  
  109.             // No longer first setter call.
  110.             _firstSetterCall = false;
  111.         }
  112.     }
  113. }
  114.  
  115. namespace System.ComponentModel.Property
  116. {
  117.     /// <summary>
  118.     ///   Option which can be passed to the setter of the Property class.
  119.     /// </summary>
  120.     public enum SetterCallbackOption
  121.     {
  122.         /// <summary>
  123.         ///   The PropertyChanged callback is called every time set is called, regardless of the old and new value.
  124.         /// </summary>
  125.         Always,
  126.         /// <summary>
  127.         ///   The PropertyChanged callback is only called when the new value is different from the old value.
  128.         /// </summary>
  129.         OnNewValue,
  130.         /// <summary>
  131.         ///   The PropertyChanged callback is only called when the new value is different from the old value,
  132.         ///   but also the first time the setter is called, regardless of the old and new value.
  133.         /// </summary>
  134.         OnNewValueAndFirst,
  135.         /// <summary>
  136.         ///   The PropertyChanged callback is only called when the new value equals the old value.
  137.         /// </summary>
  138.         OnSameValue
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement