PageRenderTime 17ms CodeModel.GetById 8ms app.highlight 6ms RepoModel.GetById 2ms 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
 16using System;
 17
 18namespace CBR.Core.Helpers
 19{
 20    /// <summary>
 21    /// Stores an <see cref="Action" /> without causing a hard reference
 22    /// to be created to the Action's owner. The owner can be garbage collected at any time.
 23    /// </summary>
 24    ////[ClassInfo(typeof(Messenger))]
 25    public class WeakAction
 26    {
 27        private readonly Action _action;
 28
 29        private WeakReference _reference;
 30
 31        /// <summary>
 32        /// Initializes a new instance of the <see cref="WeakAction" /> class.
 33        /// </summary>
 34        /// <param name="target">The action's owner.</param>
 35        /// <param name="action">The action that will be associated to this instance.</param>
 36        public WeakAction(object target, Action action)
 37        {
 38            _reference = new WeakReference(target);
 39            _action = action;
 40        }
 41
 42        /// <summary>
 43        /// Gets the Action associated to this instance.
 44        /// </summary>
 45        public Action Action
 46        {
 47            get
 48            {
 49                return _action;
 50            }
 51        }
 52
 53        /// <summary>
 54        /// Gets a value indicating whether the Action's owner is still alive, or if it was collected
 55        /// by the Garbage Collector already.
 56        /// </summary>
 57        public bool IsAlive
 58        {
 59            get
 60            {
 61                if (_reference == null)
 62                {
 63                    return false;
 64                }
 65
 66                return _reference.IsAlive;
 67            }
 68        }
 69
 70        /// <summary>
 71        /// Gets the Action's owner. This object is stored as a <see cref="WeakReference" />.
 72        /// </summary>
 73        public object Target
 74        {
 75            get
 76            {
 77                if (_reference == null)
 78                {
 79                    return null;
 80                }
 81
 82                return _reference.Target;
 83            }
 84        }
 85
 86        /// <summary>
 87        /// Executes the action. This only happens if the action's owner
 88        /// is still alive.
 89        /// </summary>
 90        public void Execute()
 91        {
 92            if (_action != null
 93                && IsAlive)
 94            {
 95                _action();
 96            }
 97        }
 98
 99        /// <summary>
100        /// Sets the reference that this instance stores to null.
101        /// </summary>
102        public void MarkForDeletion()
103        {
104            _reference = null;
105        }
106    }
107}