diff --git a/src/TinyIoC/TinyMessenger.cs b/src/TinyIoC/TinyMessenger.cs index bc266a8..0064834 100644 --- a/src/TinyIoC/TinyMessenger.cs +++ b/src/TinyIoC/TinyMessenger.cs @@ -32,21 +32,55 @@ public interface ITinyMessage object Sender { get; } } + /// + /// A TinyMessage to be published/delivered by TinyMessenger + /// + /// Type of the messenge sender + public interface ITinyMessage : ITinyMessage + where TSender: class + { + /// + /// The sender of the message, or null if not supported by the message implementation. + /// + new TSender Sender { get; } + } + + /// + /// Base class for messages that provides weak refrence storage of the sender + /// + public abstract class TinyMessageBase: TinyMessageBase + { + public TinyMessageBase(object sender) + : base(sender) + { + } + } + /// /// Base class for messages that provides weak refrence storage of the sender /// - public abstract class TinyMessageBase : ITinyMessage + /// Type of the messenge sender + public abstract class TinyMessageBase : ITinyMessage + where TSender: class { /// /// Store a WeakReference to the sender just in case anyone is daft enough to /// keep the message around and prevent the sender from being collected. /// private WeakReference _Sender; - public object Sender + public TSender Sender { get { - return (_Sender == null) ? null : _Sender.Target; + return (_Sender == null) ? null : (TSender)_Sender.Target; + } + } + + object ITinyMessage.Sender + { + get + { + return Sender; } } @@ -54,7 +88,7 @@ public object Sender /// Initializes a new instance of the MessageBase class. /// /// Message sender (usually "this") - public TinyMessageBase(object sender) + public TinyMessageBase(TSender sender) { if (sender == null) throw new ArgumentNullException("sender"); @@ -63,11 +97,25 @@ public TinyMessageBase(object sender) } } + /// + /// Generic message with user specified content + /// + /// Content type to store + public class GenericTinyMessage : GenericTinyMessage + { + public GenericTinyMessage(object sender, TContent content) + : base(sender, content) + { + } + } + /// /// Generic message with user specified content /// + /// Type of the messenge sender /// Content type to store - public class GenericTinyMessage : TinyMessageBase + public class GenericTinyMessage : TinyMessageBase + where TSender: class { /// /// Contents of the message @@ -79,7 +127,7 @@ public class GenericTinyMessage : TinyMessageBase /// /// Message sender (usually "this") /// Contents of the message - public GenericTinyMessage(object sender, TContent content) + public GenericTinyMessage(TSender sender, TContent content) : base(sender) { Content = content; @@ -90,7 +138,21 @@ public GenericTinyMessage(object sender, TContent content) /// Basic "cancellable" generic message /// /// Content type to store - public class CancellableGenericTinyMessage : TinyMessageBase + public class CancellableGenericTinyMessage : CancellableGenericTinyMessage + { + public CancellableGenericTinyMessage(object sender, TContent content, Action cancelAction) + : base(sender, content, cancelAction) + { + } + } + + /// + /// Basic "cancellable" generic message + /// + /// Type of the messenge sender + /// Content type to store + public class CancellableGenericTinyMessage : TinyMessageBase + where TSender: class { /// /// Cancel action @@ -108,7 +170,7 @@ public class CancellableGenericTinyMessage : TinyMessageBase /// Message sender (usually "this") /// Contents of the message /// Action to call for cancellation - public CancellableGenericTinyMessage(object sender, TContent content, Action cancelAction) + public CancellableGenericTinyMessage(TSender sender, TContent content, Action cancelAction) : base(sender) { if (cancelAction == null)