From 58d7bc654acbc9dec4227c4331d1d85c9f1032f4 Mon Sep 17 00:00:00 2001 From: Andale-online Date: Fri, 15 Nov 2019 10:37:01 +0100 Subject: [PATCH] GetVariable(string name, Type type) (#155) * GetVariable(string name, Type type) --- src/core/Elsa.Abstractions/Models/Variables.cs | 10 ++++++++-- .../Models/WorkflowExecutionScope.cs | 4 +++- .../Services/Models/WorkflowExecutionContext.cs | 11 +++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/core/Elsa.Abstractions/Models/Variables.cs b/src/core/Elsa.Abstractions/Models/Variables.cs index e167d2fff..b1af0644d 100644 --- a/src/core/Elsa.Abstractions/Models/Variables.cs +++ b/src/core/Elsa.Abstractions/Models/Variables.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -29,10 +30,15 @@ namespace Elsa.Models return ContainsKey(name) ? this[name] : default; } + public object GetVariable(string name, Type type) + { + var value = ContainsKey(name) ? this[name] : default; + return value == null ? default : value.ToObject(type); + } + public T GetVariable(string name) { var value = ContainsKey(name) ? this[name] : default; - return value == null ? default : value.ToObject(); } diff --git a/src/core/Elsa.Abstractions/Models/WorkflowExecutionScope.cs b/src/core/Elsa.Abstractions/Models/WorkflowExecutionScope.cs index 238a69ffb..6d3ab0ce7 100644 --- a/src/core/Elsa.Abstractions/Models/WorkflowExecutionScope.cs +++ b/src/core/Elsa.Abstractions/Models/WorkflowExecutionScope.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Linq; +using System; namespace Elsa.Models { @@ -18,6 +19,7 @@ namespace Elsa.Models } public T GetVariable(string name) => Variables.GetVariable(name); + public object GetVariable(string name, Type type) => Variables.GetVariable(name, type); public JToken GetVariable(string name) => Variables.GetVariable(name); } } \ No newline at end of file diff --git a/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs b/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs index 4bc38dfd8..1b114ff34 100644 --- a/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs +++ b/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -75,7 +75,14 @@ namespace Elsa.Services.Models var scope = Workflow.Scopes.FirstOrDefault(x => x.Variables.ContainsKey(name)) ?? CurrentScope; return scope.GetVariable(name); } - + + public object GetVariable(string name, Type type) + { + // Get the first scope (starting from the newest one) containing the variable. + var scope = Workflow.Scopes.FirstOrDefault(x => x.Variables.ContainsKey(name)) ?? CurrentScope; + return scope.GetVariable(name, type); + } + public object GetVariable(string name) { // Get the first scope (starting from the newest one) containing the variable.