PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/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
16using System;
17
18namespace CBR.Core.Helpers
19{
20    /// <summary>
21    /// Stores an 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    /// <typeparam name="T">The type of the Action's parameter.</typeparam>
25    ////[ClassInfo(typeof(Messenger))]
26    public class WeakAction<T> : WeakAction, IExecuteWithObject
27    {
28        private readonly Action<T> _action;
29
30        /// <summary>
31        /// Initializes a new instance of the WeakAction class.
32        /// </summary>
33        /// <param name="target">The action's owner.</param>
34        /// <param name="action">The action that will be associated to this instance.</param>
35        public WeakAction(object target, Action<T> action)
36            : base(target, null)
37        {
38            _action = action;
39        }
40
41        /// <summary>
42        /// Gets the Action associated to this instance.
43        /// </summary>
44        public new Action<T> Action
45        {
46            get
47            {
48                return _action;
49            }
50        }
51
52        /// <summary>
53        /// Executes the action. This only happens if the action's owner
54        /// is still alive. The action's parameter is set to default(T).
55        /// </summary>
56        public new void Execute()
57        {
58            if (_action != null
59                && IsAlive)
60            {
61                _action(default(T));
62            }
63        }
64
65        /// <summary>
66        /// Executes the action. This only happens if the action's owner
67        /// is still alive.
68        /// </summary>
69        /// <param name="parameter">A parameter to be passed to the action.</param>
70        public void Execute(T parameter)
71        {
72            if (_action != null
73                && IsAlive)
74            {
75                _action(parameter);
76            }
77        }
78
79        /// <summary>
80        /// Executes the action with a parameter of type object. This parameter
81        /// will be casted to T. This method implements <see cref="IExecuteWithObject.ExecuteWithObject" />
82        /// and can be useful if you store multiple WeakAction{T} instances but don't know in advance
83        /// what type T represents.
84        /// </summary>
85        /// <param name="parameter">The parameter that will be passed to the action after
86        /// being casted to T.</param>
87        public void ExecuteWithObject(object parameter)
88        {
89            var parameterCasted = (T) parameter;
90            Execute(parameterCasted);
91        }
92    }
93}