From 9f5ce2d484d955e2c77fa612a57e486f02e3fc86 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 9 Jan 2021 22:18:22 -0500 Subject: [PATCH] implement ISupportsExtraData for Command --- MBS.Framework/Command.cs | 33 +++++++++++++++- MBS.Framework/ISupportsExtraData.cs | 60 +++++++++++++++++++++++++++++ MBS.Framework/MBS.Framework.csproj | 3 ++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 MBS.Framework/ISupportsExtraData.cs diff --git a/MBS.Framework/Command.cs b/MBS.Framework/Command.cs index 33f9daf..1802cb7 100644 --- a/MBS.Framework/Command.cs +++ b/MBS.Framework/Command.cs @@ -1,8 +1,9 @@ using System; +using System.Collections.Generic; namespace MBS.Framework { - public class Command + public class Command : ISupportsExtraData { public class CommandCollection : System.Collections.ObjectModel.Collection @@ -99,6 +100,36 @@ namespace MBS.Framework { return String.Format("{0} [{1}]", ID, Title); } + + private Dictionary _extraData = new Dictionary(); + public T GetExtraData(string key, T defaultValue = default(T)) + { + if (_extraData.ContainsKey(key)) + { + if (_extraData[key] is T) + { + return (T)_extraData[key]; + } + } + return defaultValue; + } + + public void SetExtraData(string key, T value) + { + _extraData[key] = value; + } + + public object GetExtraData(string key, object defaultValue = null) + { + if (_extraData.ContainsKey(key)) + return _extraData[key]; + return defaultValue; + } + + public void SetExtraData(string key, object value) + { + _extraData[key] = value; + } } } diff --git a/MBS.Framework/ISupportsExtraData.cs b/MBS.Framework/ISupportsExtraData.cs new file mode 100644 index 0000000..85f0400 --- /dev/null +++ b/MBS.Framework/ISupportsExtraData.cs @@ -0,0 +1,60 @@ +// +// ISupportsExtraData.cs - interface for providing GetExtraData / SetExtraData methods +// +// Author: +// Michael Becker +// +// Copyright (c) 2019-2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework +{ + /// + /// Provides methods to store and retrieve extra data parameters on objects. + /// + public interface ISupportsExtraData + { + /// + /// Gets the extra data with the specified key. If the extra data with the given key is not present, + /// return the specified default value. + /// + /// The extra data with the specified key, or if not present. + /// The key to look up. + /// The default value to return if the key is not present. + /// The type of data item to return. + T GetExtraData(string key, T defaultValue = default(T)); + /// + /// Sets the extra data with the specified key and value. + /// + /// The name of the data item to set. + /// The value to set. + /// The type of data item to set. + void SetExtraData(string key, T value); + /// + /// Gets the extra data with the specified key. If the extra data with the given key is not present, + /// return the specified default value. + /// + /// The extra data with the specified key, or if not present. + /// The key to look up. + /// The default value to return if the key is not present. + object GetExtraData(string key, object defaultValue = null); + /// + /// Sets the extra data with the specified key and value. + /// + /// The name of the data item to set. + /// The value to set. + void SetExtraData(string key, object value); + } +} diff --git a/MBS.Framework/MBS.Framework.csproj b/MBS.Framework/MBS.Framework.csproj index 61da8ad..ca7a53c 100644 --- a/MBS.Framework/MBS.Framework.csproj +++ b/MBS.Framework/MBS.Framework.csproj @@ -11,6 +11,8 @@ 2.0 true ..\..\Production.snk + 4.0.2021.01 + false true @@ -92,6 +94,7 @@ +