Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ended up using the following activity. It can't access the workflow's variables, instead it accepts a single argument 'Value' that can be used by the same name inside the dynamic expression. Other than that it works pretty well.</p> <pre><code>public class Evaluate&lt;TIn, TOut&gt; : NativeActivity&lt;TOut&gt; { [RequiredArgument] public InArgument&lt;string&gt; ExpressionText { get; set; } [RequiredArgument] public InArgument&lt;TIn&gt; Value { get; set; } protected override void Execute(NativeActivityContext context) { var result = new ExpressionEvaluator&lt;TIn, TOut&gt;(ExpressionText.Get(context)).EvalWith(Value.Get(context)); Result.Set(context, result); } } public class ExpressionEvaluator&lt;TIn, TOut&gt; : Activity&lt;TOut&gt; { [RequiredArgument] public InArgument&lt;TIn&gt; Value { get; set; } public ExpressionEvaluator(string predicate) { VisualBasic.SetSettingsForImplementation(this, VbSettings); Implementation = () =&gt; new Assign&lt;TOut&gt; { Value = new InArgument&lt;TOut&gt;(new VisualBasicValue&lt;TOut&gt;(predicate)), To = new ArgumentReference&lt;TOut&gt;("Result") }; } public TOut EvalWith(TIn value) { return WorkflowInvoker.Invoke(this, new Dictionary&lt;string, object&gt; { { "Value", value } }); } private static readonly VisualBasicSettings VbSettings; static ExpressionEvaluator() { VbSettings = new VisualBasicSettings(); AddImports(typeof(TIn), VbSettings.ImportReferences); AddImports(typeof(TOut), VbSettings.ImportReferences); } private static void AddImports(Type type, ISet&lt;VisualBasicImportReference&gt; imports) { if (type.IsPrimitive || type == typeof(void) || type.Namespace == "System") return; var wasAdded = imports.Add(new VisualBasicImportReference { Assembly = type.Assembly.GetName().Name, Import = type.Namespace }); if (!wasAdded) return; if (type.BaseType != null) AddImports(type.BaseType, imports); foreach (var interfaceType in type.GetInterfaces()) AddImports(interfaceType, imports); foreach (var property in type.GetProperties()) AddImports(property.PropertyType, imports); foreach (var method in type.GetMethods()) { AddImports(method.ReturnType, imports); foreach (var parameter in method.GetParameters()) AddImports(parameter.ParameterType, imports); if (method.IsGenericMethod) { foreach (var genericArgument in method.GetGenericArguments()) AddImports(genericArgument, imports); } } if (type.IsGenericType) { foreach (var genericArgument in type.GetGenericArguments()) AddImports(genericArgument, imports); } } } </code></pre> <p><strong>EDIT</strong>: Updated the class to include complete assembly and namespace imports, lest you get the dreaded (and unhelpful) error message:</p> <blockquote> <p>'Value' is not declared. It may be inaccessible due to its protection level.</p> </blockquote> <p>Also, moved the ExpressionEvaluator class outside and made it public, so you can used it outside of WF, like so:</p> <pre><code>new ExpressionEvaluator&lt;int, double&gt;("Value * Math.PI").EvalWith(2); </code></pre> <p>Which will return:</p> <blockquote> <p>6.28318530717959</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload