PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 107 lines | 55 code | 11 blank | 41 comment | 7 complexity | a3ee371b533c85d2f9cb37ec7b93dc95 MD5 | raw file
  1. // ****************************************************************************
  2. // <copyright file="WeakAction.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 <see cref="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. ////[ClassInfo(typeof(Messenger))]
  23. public class WeakAction
  24. {
  25. private readonly Action _action;
  26. private WeakReference _reference;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="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 action)
  33. {
  34. _reference = new WeakReference(target);
  35. _action = action;
  36. }
  37. /// <summary>
  38. /// Gets the Action associated to this instance.
  39. /// </summary>
  40. public Action Action
  41. {
  42. get
  43. {
  44. return _action;
  45. }
  46. }
  47. /// <summary>
  48. /// Gets a value indicating whether the Action's owner is still alive, or if it was collected
  49. /// by the Garbage Collector already.
  50. /// </summary>
  51. public bool IsAlive
  52. {
  53. get
  54. {
  55. if (_reference == null)
  56. {
  57. return false;
  58. }
  59. return _reference.IsAlive;
  60. }
  61. }
  62. /// <summary>
  63. /// Gets the Action's owner. This object is stored as a <see cref="WeakReference" />.
  64. /// </summary>
  65. public object Target
  66. {
  67. get
  68. {
  69. if (_reference == null)
  70. {
  71. return null;
  72. }
  73. return _reference.Target;
  74. }
  75. }
  76. /// <summary>
  77. /// Executes the action. This only happens if the action's owner
  78. /// is still alive.
  79. /// </summary>
  80. public void Execute()
  81. {
  82. if (_action != null
  83. && IsAlive)
  84. {
  85. _action();
  86. }
  87. }
  88. /// <summary>
  89. /// Sets the reference that this instance stores to null.
  90. /// </summary>
  91. public void MarkForDeletion()
  92. {
  93. _reference = null;
  94. }
  95. }
  96. }