* GetVariable<T> now uses Convert.ChangeType Fixes issue where saving an Int32, serialising and deserialising, then accessing with GetVariable<int>(...) no longer causes type cast exception. The saved value is a JValue which treats Int32 as Int64. Convert.ChangeType has a better chance of performing conversion without error. Also returns default of T if the variable isn't found. * Added SetVariable overload in Variables To prevent double-wrapping objects in more than 1 Variable object
66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace Elsa.Models
|
|
{
|
|
public class Variables : Dictionary<string, Variable>
|
|
{
|
|
public static readonly Variables Empty = new Variables();
|
|
|
|
public Variables()
|
|
{
|
|
}
|
|
|
|
public Variables(Variables other) : this((IEnumerable<KeyValuePair<string, Variable>>) other)
|
|
{
|
|
}
|
|
|
|
public Variables(IEnumerable<KeyValuePair<string, Variable>> dictionary)
|
|
{
|
|
foreach (var item in dictionary)
|
|
this[item.Key] = item.Value;
|
|
}
|
|
|
|
public Variables(IEnumerable<KeyValuePair<string, object>> dictionary)
|
|
{
|
|
foreach (var item in dictionary)
|
|
SetVariable(item.Key, item.Value);
|
|
}
|
|
|
|
public object GetVariable(string name)
|
|
{
|
|
return ContainsKey(name) ? this[name].Value : default;
|
|
}
|
|
|
|
public T GetVariable<T>(string name)
|
|
{
|
|
object value = GetVariable(name);
|
|
return (value != default)
|
|
? (T)System.Convert.ChangeType(value, typeof(T))
|
|
: default(T);
|
|
}
|
|
|
|
public Variable SetVariable(string name, object value)
|
|
{
|
|
return this[name] = new Variable(value);
|
|
}
|
|
|
|
public Variable SetVariable(string name, Variable variable)
|
|
{
|
|
return SetVariable(name, variable.Value);
|
|
}
|
|
|
|
public void SetVariables(Variables variables) =>
|
|
SetVariables((IEnumerable<KeyValuePair<string, Variable>>) variables);
|
|
|
|
public void SetVariables(IEnumerable<KeyValuePair<string, Variable>> variables)
|
|
{
|
|
foreach (var variable in variables)
|
|
SetVariable(variable.Key, variable.Value);
|
|
}
|
|
|
|
public bool HasVariable(string name)
|
|
{
|
|
return ContainsKey(name);
|
|
}
|
|
}
|
|
} |