public static void CompileExpressions(DynamicActivity dynamicActivity, params Assembly[] references) { // See https://docs.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/csharp-expressions string activityName = dynamicActivity.Name; string activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot"; string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse()); TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings { Activity = dynamicActivity, Language = "C#", ActivityName = activityType, ActivityNamespace = activityNamespace, RootNamespace = null, GenerateAsPartialClass = false, AlwaysGenerateSource = true, ForImplementation = true }; // add assembly references TextExpression.SetReferencesForImplementation(dynamicActivity, references.Select(a => (AssemblyReference)a).ToList()); // Compile the C# expression. var results = new TextExpressionCompiler(settings).Compile(); if (results.HasErrors) { throw new Exception("Compilation failed."); } // attach compilation result to live activity var compiledExpression = (ICompiledExpressionRoot)Activator.CreateInstance(results.ResultType, new object[] { dynamicActivity }); CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpression); }
public void SetNamespaces(object target) { var dev2ActivitiesAssembly = typeof(WorkflowHelper).Assembly; var dev2CommonAssembly = typeof(GlobalConstants).Assembly; var dev2DataAssembly = typeof(Dev2DataListDecisionHandler).Assembly; var namespaces = new Dictionary <string, Assembly> { { "Dev2.Common", dev2CommonAssembly }, { "Dev2.Data.Decisions.Operations", dev2DataAssembly }, { "Dev2.Data.SystemTemplates.Models", dev2DataAssembly }, { "Dev2.DataList.Contract", dev2DataAssembly }, { "Dev2.DataList.Contract.Binary_Objects", dev2DataAssembly }, { "Unlimited.Applications.BusinessDesignStudio.Activities", dev2ActivitiesAssembly } }; #region Set C# assembly references // http://stackoverflow.com/questions/16086612/wf-4-5-using-c-sharp-expressions-with-external-class-references // http://blogs.msdn.com/b/tilovell/archive/2012/05/25/wf4-5-using-csharpvalue-lt-t-gt-and-csharpreference-lt-t-gt-in-net-4-5-compiling-expressions-and-changes-in-visual-studio-generated-xaml.aspx TextExpression.SetReferencesForImplementation(target, namespaces.Values.Distinct().Select(a => new AssemblyReference { Assembly = a }).ToArray()); var impl = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation"); AttachablePropertyServices.SetProperty(target, impl, namespaces.Keys.ToList()); #endregion #region Set VB assembly references // var vbSettings = VisualBasic.GetSettings(target) ?? new VisualBasicSettings(); // vbSettings.ImportReferences.Clear(); // // foreach(var ns in namespaces.Keys) // { // try // { // vbSettings.ImportReferences.Add(new VisualBasicImportReference { Assembly = namespaces[ns].GetName().Name, Import = ns }); // } // catch(Exception e) // { // Dev2Logger.Error(e.Message,e); // } // } // // VisualBasic.SetSettings(target, vbSettings); #endregion }
internal static void SetTextExpressionNamespaces(object root, IList <string> namespaces, IList <AssemblyReference> references) { if (NamespaceHelper.ShouldUsePropertiesForImplementation(root)) { TextExpression.SetNamespacesForImplementation(root, namespaces); TextExpression.SetReferencesForImplementation(root, references); } else { TextExpression.SetNamespaces(root, namespaces); TextExpression.SetReferences(root, references); } }
static DynamicActivity GetDynamicActivity(ActivityBuilder activityDefinition) { DynamicActivity result = new DynamicActivity { Name = activityDefinition.Name }; foreach (DynamicActivityProperty property in activityDefinition.Properties) { result.Properties.Add(property); } foreach (Attribute attrib in activityDefinition.Attributes) { result.Attributes.Add(attrib); } foreach (Constraint constraint in activityDefinition.Constraints) { result.Constraints.Add(constraint); } result.Implementation = () => activityDefinition.Implementation; VisualBasicSettings vbsettings = VisualBasic.GetSettings(activityDefinition); if (vbsettings != null) { VisualBasic.SetSettings(result, vbsettings); } IList <string> namespacesForImplementation = TextExpression.GetNamespacesForImplementation(activityDefinition); if (namespacesForImplementation.Count > 0) { TextExpression.SetNamespacesForImplementation(result, namespacesForImplementation); } IList <AssemblyReference> referencesForImplementation = TextExpression.GetReferencesForImplementation(activityDefinition); if (referencesForImplementation.Count > 0) { TextExpression.SetReferencesForImplementation(result, referencesForImplementation); } return(result); }
private static DynamicActivity GetDynamicActivity(ActivityBuilder activityDefinition) { var result = new DynamicActivity { Name = activityDefinition.Name }; foreach (var property in activityDefinition.Properties) { result.Properties.Add(property); } foreach (var attrib in activityDefinition.Attributes) { result.Attributes.Add(attrib); } foreach (var constraint in activityDefinition.Constraints) { result.Constraints.Add(constraint); } result.Implementation = () => activityDefinition.Implementation; var vbsettings = VisualBasic.GetSettings(activityDefinition); if (vbsettings != null) { VisualBasic.SetSettings(result, vbsettings); } var namespacesForImplementation = TextExpression.GetNamespacesForImplementation(activityDefinition); if (namespacesForImplementation.Count > 0) { TextExpression.SetNamespacesForImplementation(result, namespacesForImplementation); } var referencesForImplementation = TextExpression.GetReferencesForImplementation(activityDefinition); if (referencesForImplementation.Count > 0) { TextExpression.SetReferencesForImplementation(result, referencesForImplementation); } return(result); }
static void Main() { var errorCodeWorkflow = new DynamicActivity { Name = "MyScenario.MyDynamicActivity3", Properties = { new DynamicActivityProperty { Name = "Address", Type = typeof(InArgument <MailAddress>), }, }, Implementation = () => new WriteLine { Text = new CSharpValue <String> { ExpressionText = "\"MyDynamicActivity \" + Address.DisplayName" } } }; var impl = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation"); var namespaces = new List <string> { typeof(MailAddress).Namespace }; TextExpression.SetReferencesForImplementation(errorCodeWorkflow, new AssemblyReference { Assembly = typeof(MailAddress).Assembly }); AttachablePropertyServices.SetProperty(errorCodeWorkflow, impl, namespaces); CompileExpressions(errorCodeWorkflow); WorkflowInvoker.Invoke(errorCodeWorkflow, new Dictionary <String, Object> { { "Address", new MailAddress { DisplayName = "TestDisplayName" } } }); }
public void Compile(string activityNamespace, string activityName, Activity activity, bool forImplementation = true) { if (_refAssemblies != null) { var references = _refAssemblies.Select(a => new AssemblyReference { Assembly = a }).ToArray(); if (!forImplementation) { TextExpression.SetReferences(activity, references); } else { TextExpression.SetReferencesForImplementation(activity, references); } } if (_usingNamespaces != null) { var namespaces = _usingNamespaces.ToArray(); if (!forImplementation) { TextExpression.SetNamespaces(activity, namespaces); } else { TextExpression.SetNamespacesForImplementation(activity, namespaces); } } var settings = new TextExpressionCompilerSettings { Activity = activity, Language = "C#", ActivityName = activityName, ActivityNamespace = activityNamespace, RootNamespace = "dd", GenerateAsPartialClass = false, AlwaysGenerateSource = false, ForImplementation = forImplementation, Compiler = new CSharpAheadOfTimeCompiler(), }; var results = new TextExpressionCompiler(settings).Compile(); if (results.HasErrors) { throw new Exception("Compilation failed."); } if (results.ResultType == null) { return; } var compiledExpressionRoot = Activator.CreateInstance(results.ResultType, activity) as ICompiledExpressionRoot; if (!forImplementation) { CompiledExpressionInvoker.SetCompiledExpressionRoot(activity, compiledExpressionRoot); } else { CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(activity, compiledExpressionRoot); } }