/// <summary> /// Apply the changes /// </summary> /// <param name="instance"></param> /// <param name="wfChanges"></param> private static void ValidateAndApplyChanges( WorkflowInstance instance, WorkflowChanges wfChanges) { //validate the structural changes before applying them ValidationErrorCollection errors = wfChanges.Validate(); if (errors.Count == 0) { try { //apply the changes to the workflow instance instance.ApplyWorkflowChanges(wfChanges); } catch (Exception e) { Console.WriteLine("Exception applying changes: {0}", e.Message); } } else { //the proposed changes are not valid foreach (ValidationError error in errors) { Console.WriteLine(error.ToString()); } } }
/// <summary> /// Add a new custom activity to this workflow instance /// </summary> /// <param name="instance"></param> private void AddNewActivity() { //create an instance of the specified new activity if (NewActivityType != null && NumberProperty != null) { //create a workflow changes object WorkflowChanges wfChanges = new WorkflowChanges(this); //find the SequenceActivity that is a placeholder //for new activities CompositeActivity placeholder = wfChanges.TransientWorkflow.GetActivityByName( "sequencePlaceholder") as CompositeActivity; if (placeholder == null) { return; } //construct an instance of the activity //using reflection ConstructorInfo ctor = NewActivityType.GetConstructor(Type.EmptyTypes); Activity newActivity = ctor.Invoke(null) as Activity; //bind the TestNumber property of the activity //to the TestNumber property of the workflow newActivity.SetBinding(NumberProperty, new ActivityBind(this.Name, "TestNumber")); //add the new custom activity to the workflow placeholder.Activities.Add(newActivity); //validate the structural changes before applying them ValidationErrorCollection errors = wfChanges.Validate(); if (errors.Count == 0) { //apply the changes to the workflow instance this.ApplyWorkflowChanges(wfChanges); } else { //the proposed changes are not valid foreach (ValidationError error in errors) { Console.WriteLine(error.ToString()); } } } }