1 namespace System.Workflow.Activities 2 { 3 using System; 4 using System.Text; 5 using System.Reflection; 6 using System.Collections; 7 using System.CodeDom; 8 using System.ComponentModel; 9 using System.ComponentModel.Design; 10 using System.Drawing; 11 using System.Workflow.ComponentModel; 12 using System.Workflow.ComponentModel.Design; 13 using System.Workflow.ComponentModel.Serialization; 14 using System.Collections.Generic; 15 using System.Workflow.ComponentModel.Compiler; 16 17 [SRDescription(SR.CodeActivityDescription)] 18 [ToolboxItem(typeof(ActivityToolboxItem))] 19 [Designer(typeof(CodeDesigner), typeof(IDesigner))] 20 [ToolboxBitmap(typeof(CodeActivity), "Resources.code.png")] 21 [DefaultEvent("ExecuteCode")] 22 [SRCategory(SR.Standard)] 23 [ActivityValidator(typeof(CodeActivityValidator))] 24 [Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")] 25 public sealed class CodeActivity : Activity 26 { 27 #region Constructors 28 CodeActivity()29 public CodeActivity() 30 { 31 } 32 CodeActivity(string name)33 public CodeActivity(string name) 34 : base(name) 35 { 36 } 37 38 #endregion 39 40 public static readonly DependencyProperty ExecuteCodeEvent = DependencyProperty.Register("ExecuteCode", typeof(EventHandler), typeof(CodeActivity)); 41 Execute(ActivityExecutionContext executionContext)42 protected override sealed ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) 43 { 44 base.RaiseEvent(CodeActivity.ExecuteCodeEvent, this, EventArgs.Empty); 45 46 return ActivityExecutionStatus.Closed; 47 } 48 49 [SRCategory(SR.Handlers)] 50 [SRDescription(SR.UserCodeHandlerDescr)] 51 [MergableProperty(false)] 52 public event EventHandler ExecuteCode 53 { 54 add 55 { 56 base.AddHandler(ExecuteCodeEvent, value); 57 } 58 remove 59 { 60 base.RemoveHandler(ExecuteCodeEvent, value); 61 } 62 } 63 64 private class CodeActivityValidator : ActivityValidator 65 { Validate(ValidationManager manager, object obj)66 public override ValidationErrorCollection Validate(ValidationManager manager, object obj) 67 { 68 ValidationErrorCollection errors = new ValidationErrorCollection(); 69 70 CodeActivity code = obj as CodeActivity; 71 if (code == null) 72 throw new InvalidOperationException(); 73 74 // This violates the P || C validation condition, but we are compiling with csc.exe here! 75 if (code.GetInvocationList<EventHandler>(CodeActivity.ExecuteCodeEvent).Length == 0 && 76 code.GetBinding(CodeActivity.ExecuteCodeEvent) == null) 77 { 78 Hashtable hashtable = code.GetValue(WorkflowMarkupSerializer.EventsProperty) as Hashtable; 79 if (hashtable == null || hashtable["ExecuteCode"] == null) 80 errors.Add(ValidationError.GetNotSetValidationError("ExecuteCode")); 81 } 82 83 errors.AddRange(base.Validate(manager, obj)); 84 return errors; 85 } 86 } 87 } 88 } 89