Note that there are some explanatory texts on larger screens.

plurals
  1. POWF 4.5. Using C# expressions with external class references
    primarykey
    data
    text
    <p>I am trying to compile Dynamic activity using the new WF 4.5 features - C# expression. It works until I add an external class in the expression.</p> <p>If the expression contains basic objects it's complied. If I add a reference to an external class it's generates an error "The type or namespace name 'xxxxx' could not be found (are you missing a using directive or an assembly reference?)"</p> <p>So, the question is how can I reference external classes for C# expressions?</p> <p>P.S. It works fine with VisualBasic expression type</p> <p>Thanks</p> <pre><code>//Compiled without errors var errorCodeWorkflow = new DynamicActivity { Name = "DynamicActivity", Implementation = () =&gt; new WriteLine { Text = new CSharpValue&lt;String&gt; { ExpressionText = "new Random().Next(1, 101).ToString()" } } }; CompileExpressions(errorCodeWorkflow); WorkflowInvoker.Invoke(errorCodeWorkflow); //Error using System; using System.Activities; using System.Activities.Expressions; using System.Activities.Statements; using System.Activities.XamlIntegration; using System.Collections.Generic; using System.Linq; using Microsoft.CSharp.Activities; namespace CSharpExpression { class Program { static void Main() { var errorCodeWorkflow = new DynamicActivity { Name = "MyScenario.MyDynamicActivity3", Properties = { new DynamicActivityProperty { Name = "Address", Type = typeof(InArgument&lt;MailAddress&gt;), }, }, Implementation = () =&gt; new WriteLine { Text = new CSharpValue&lt;String&gt; { ExpressionText = "\"MyDynamicActivity \" + Address.DisplayName" } } }; CompileExpressions(errorCodeWorkflow); WorkflowInvoker.Invoke(errorCodeWorkflow, new Dictionary&lt;String, Object&gt; { { "Address", new MailAddress { DisplayName = "TestDisplayName" } } }); } static void CompileExpressions(DynamicActivity dynamicActivity) { var activityName = dynamicActivity.Name; var activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot"; var activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse()); var settings = new TextExpressionCompilerSettings { Activity = dynamicActivity, Language = "C#", ActivityName = activityType, ActivityNamespace = activityNamespace, RootNamespace = "CSharpExpression", GenerateAsPartialClass = false, AlwaysGenerateSource = true, ForImplementation = true }; var results = new TextExpressionCompiler(settings).Compile(); if (results.HasErrors) { throw new Exception("Compilation failed."); } var compiledExpressionRoot = Activator.CreateInstance(results.ResultType, new object[] { dynamicActivity }) as ICompiledExpressionRoot; CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot); } } public class MailAddress { public String Address { get; set; } public String DisplayName { get; set; } } } </code></pre>
    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.
 

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