implement ISupportsExtraData for Command

This commit is contained in:
Michael Becker 2021-01-09 22:18:22 -05:00
parent 83051d779f
commit 9f5ce2d484
No known key found for this signature in database
GPG Key ID: 98C333A81F18C22C
3 changed files with 95 additions and 1 deletions

View File

@ -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<Command>
@ -99,6 +100,36 @@ namespace MBS.Framework
{
return String.Format("{0} [{1}]", ID, Title);
}
private Dictionary<string, object> _extraData = new Dictionary<string, object>();
public T GetExtraData<T>(string key, T defaultValue = default(T))
{
if (_extraData.ContainsKey(key))
{
if (_extraData[key] is T)
{
return (T)_extraData[key];
}
}
return defaultValue;
}
public void SetExtraData<T>(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;
}
}
}

View File

@ -0,0 +1,60 @@
//
// ISupportsExtraData.cs - interface for providing GetExtraData / SetExtraData methods
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
using System;
namespace MBS.Framework
{
/// <summary>
/// Provides methods to store and retrieve extra data parameters on objects.
/// </summary>
public interface ISupportsExtraData
{
/// <summary>
/// Gets the extra data with the specified key. If the extra data with the given key is not present,
/// return the specified default value.
/// </summary>
/// <returns>The extra data with the specified key, or <paramref name="defaultValue" /> if not present.</returns>
/// <param name="key">The key to look up.</param>
/// <param name="defaultValue">The default value to return if the key is not present.</param>
/// <typeparam name="T">The type of data item to return.</typeparam>
T GetExtraData<T>(string key, T defaultValue = default(T));
/// <summary>
/// Sets the extra data with the specified key and value.
/// </summary>
/// <param name="key">The name of the data item to set.</param>
/// <param name="value">The value to set.</param>
/// <typeparam name="T">The type of data item to set.</typeparam>
void SetExtraData<T>(string key, T value);
/// <summary>
/// Gets the extra data with the specified key. If the extra data with the given key is not present,
/// return the specified default value.
/// </summary>
/// <returns>The extra data with the specified key, or <paramref name="defaultValue" /> if not present.</returns>
/// <param name="key">The key to look up.</param>
/// <param name="defaultValue">The default value to return if the key is not present.</param>
object GetExtraData(string key, object defaultValue = null);
/// <summary>
/// Sets the extra data with the specified key and value.
/// </summary>
/// <param name="key">The name of the data item to set.</param>
/// <param name="value">The value to set.</param>
void SetExtraData(string key, object value);
}
}

View File

@ -11,6 +11,8 @@
<SchemaVersion>2.0</SchemaVersion>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\Production.snk</AssemblyOriginatorKeyFile>
<ReleaseVersion>4.0.2021.01</ReleaseVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -92,6 +94,7 @@
<Compile Include="Scripting\Strings\StringComponents\LiteralStringComponent.cs" />
<Compile Include="Scripting\ScriptEnvironment.cs" />
<Compile Include="Collections\Generic\ExtensionMethods.cs" />
<Compile Include="ISupportsExtraData.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Logic\" />