PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/CBR/CBR.Core/Helpers/MVVM/Events/WeakActionGeneric.cs

#
C# | 93 lines | 41 code | 7 blank | 45 comment | 6 complexity | 4582b58d42b48e13c0f65f4eeed8f490 MD5 | raw file
  1. // ****************************************************************************
  2. // <copyright file="WeakActionGeneric.cs" company="GalaSoft Laurent Bugnion">
  3. // Copyright © GalaSoft Laurent Bugnion 2009-2011
  4. // </copyright>
  5. // ****************************************************************************
  6. // <author>Laurent Bugnion</author>
  7. // <email>laurent@galasoft.ch</email>
  8. // <date>18.9.2009</date>
  9. // <project>GalaSoft.MvvmLight</project>
  10. // <web>http://www.galasoft.ch</web>
  11. // <license>
  12. // See license.txt in this solution or http://www.galasoft.ch/license_MIT.txt
  13. // </license>
  14. // ****************************************************************************
  15. using System;
  16. namespace CBR.Core.Helpers
  17. {
  18. /// <summary>
  19. /// Stores an Action without causing a hard reference
  20. /// to be created to the Action's owner. The owner can be garbage collected at any time.
  21. /// </summary>
  22. /// <typeparam name="T">The type of the Action's parameter.</typeparam>
  23. ////[ClassInfo(typeof(Messenger))]
  24. public class WeakAction<T> : WeakAction, IExecuteWithObject
  25. {
  26. private readonly Action<T> _action;
  27. /// <summary>
  28. /// Initializes a new instance of the WeakAction class.
  29. /// </summary>
  30. /// <param name="target">The action's owner.</param>
  31. /// <param name="action">The action that will be associated to this instance.</param>
  32. public WeakAction(object target, Action<T> action)
  33. : base(target, null)
  34. {
  35. _action = action;
  36. }
  37. /// <summary>
  38. /// Gets the Action associated to this instance.
  39. /// </summary>
  40. public new Action<T> Action
  41. {
  42. get
  43. {
  44. return _action;
  45. }
  46. }
  47. /// <summary>
  48. /// Executes the action. This only happens if the action's owner
  49. /// is still alive. The action's parameter is set to default(T).
  50. /// </summary>
  51. public new void Execute()
  52. {
  53. if (_action != null
  54. && IsAlive)
  55. {
  56. _action(default(T));
  57. }
  58. }
  59. /// <summary>
  60. /// Executes the action. This only happens if the action's owner
  61. /// is still alive.
  62. /// </summary>
  63. /// <param name="parameter">A parameter to be passed to the action.</param>
  64. public void Execute(T parameter)
  65. {
  66. if (_action != null
  67. && IsAlive)
  68. {
  69. _action(parameter);
  70. }
  71. }
  72. /// <summary>
  73. /// Executes the action with a parameter of type object. This parameter
  74. /// will be casted to T. This method implements <see cref="IExecuteWithObject.ExecuteWithObject" />
  75. /// and can be useful if you store multiple WeakAction{T} instances but don't know in advance
  76. /// what type T represents.
  77. /// </summary>
  78. /// <param name="parameter">The parameter that will be passed to the action after
  79. /// being casted to T.</param>
  80. public void ExecuteWithObject(object parameter)
  81. {
  82. var parameterCasted = (T) parameter;
  83. Execute(parameterCasted);
  84. }
  85. }
  86. }