Conditional.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WF / Activities / Conditional.cs / 1305376 / Conditional.cs

                            namespace System.Workflow.Activities 
{
    #region Imports

    using System; 
    using System.Text;
    using System.Reflection; 
    using System.Collections; 
    using System.CodeDom;
    using System.Collections.Generic; 
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Drawing.Drawing2D; 
    using System.Drawing.Design;
    using System.Workflow.ComponentModel; 
    using System.Runtime.Serialization; 
    using System.Workflow.ComponentModel.Compiler;
    using System.Workflow.Activities.Common; 

    #endregion

 
    [SRDescription(SR.ConditionalActivityDescription)]
    [ToolboxItem(typeof(IfElseToolboxItem))] 
    [Designer(typeof(IfElseDesigner), typeof(IDesigner))] 
    [SRCategory(SR.Standard)]
    [ToolboxBitmap(typeof(IfElseActivity), "Resources.Decision.png")] 
    [ActivityValidator(typeof(IfElseValidator))]
    public sealed class IfElseActivity: CompositeActivity , IActivityEventListener
    {
 
        #region Constructors
 
        public IfElseActivity() 
        {
        } 

        public IfElseActivity(string name)
            : base(name)
        { 
        }
 
        #endregion 

        public IfElseBranchActivity AddBranch(ICollection activities) 
        {
            if (activities == null)
                throw new ArgumentNullException("activities");
 
            return AddBranch(activities, null);
        } 
 
        public IfElseBranchActivity AddBranch(ICollection activities, ActivityCondition branchCondition)
        { 
            if (activities == null)
                throw new ArgumentNullException("activities");

            if (!this.DesignMode) 
                throw new InvalidOperationException(SR.GetString(SR.Error_ConditionalBranchUpdateAtRuntime));
 
            IfElseBranchActivity branchActivity = new IfElseBranchActivity(); 

            foreach(Activity activity in activities) 
                branchActivity.Activities.Add(activity);

            branchActivity.Condition = branchCondition;
            this.Activities.Add(branchActivity); 

            return branchActivity; 
        } 

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) 
        {
            if (executionContext == null)
                throw new ArgumentNullException("executionContext");
 
            bool noneTrue = true;
 
            for (int i = 0; i < this.EnabledActivities.Count; ++i) 
            {
                IfElseBranchActivity branch = this.EnabledActivities[i] as IfElseBranchActivity; 

                // Else case dont have enable condition so find them and enable it
                if (branch.Condition == null || branch.Condition.Evaluate(branch, executionContext))
                { 
                    noneTrue = false;
                    branch.RegisterForStatusChange(Activity.ClosedEvent, this); 
                    executionContext.ExecuteActivity(branch); 
                    break;
                } 
            }

            return noneTrue ? ActivityExecutionStatus.Closed : ActivityExecutionStatus.Executing;
        } 

        protected override ActivityExecutionStatus Cancel(ActivityExecutionContext executionContext) 
        { 
            bool canCloseNow = true;
 
            for (int i = 0; i < this.EnabledActivities.Count; ++i)
            {
                Activity childBranch = this.EnabledActivities[i];
 
                if (childBranch.ExecutionStatus == ActivityExecutionStatus.Executing)
                { 
                    canCloseNow = false; 
                    executionContext.CancelActivity(childBranch);
                    break; 
                }
                else if (childBranch.ExecutionStatus == ActivityExecutionStatus.Canceling || childBranch.ExecutionStatus == ActivityExecutionStatus.Faulting)
                {
                    canCloseNow = false; 
                    break;
                } 
            } 
            return canCloseNow ? ActivityExecutionStatus.Closed : ActivityExecutionStatus.Canceling;
        } 

        void IActivityEventListener.OnEvent(object sender, ActivityExecutionStatusChangedEventArgs e)
        {
            if (sender == null) 
                throw new ArgumentNullException("sender");
            if (e == null) 
                throw new ArgumentNullException("e"); 

            ActivityExecutionContext context = sender as ActivityExecutionContext; 

            if (context == null)
                throw new ArgumentException(SR.Error_SenderMustBeActivityExecutionContext, "sender");
 
            context.CloseActivity();
        } 
    } 

    internal sealed class IfElseValidator : CompositeActivityValidator 
    {
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            ValidationErrorCollection validationErrors = base.Validate(manager, obj); 

            IfElseActivity ifElse = obj as IfElseActivity; 
            if (ifElse == null) 
                throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(IfElseActivity).FullName), "obj");
 
            // Validate number of children
            if (ifElse.EnabledActivities.Count < 1)
                validationErrors.Add(new ValidationError(SR.GetString(SR.Error_ConditionalLessThanOneChildren), ErrorNumbers.Error_IfElseLessThanOneChildren));
 
            // all child activities must be IfElse branch
            foreach (Activity activity in ifElse.EnabledActivities) 
            { 
                if (!(activity is IfElseBranchActivity))
                { 
                    validationErrors.Add(new ValidationError(SR.GetString(SR.Error_ConditionalDeclNotAllConditionalBranchDecl), ErrorNumbers.Error_IfElseNotAllIfElseBranchDecl));
                    break;
                }
            } 
            return validationErrors;
        } 
 
        public override ValidationError ValidateActivityChange(Activity activity, ActivityChangeAction action)
        { 
            if (activity == null)
                throw new ArgumentNullException("activity");
            if (action == null)
                throw new ArgumentNullException("action"); 

            if (activity.ExecutionStatus != ActivityExecutionStatus.Initialized && 
                 activity.ExecutionStatus != ActivityExecutionStatus.Closed) 
            {
                return new ValidationError(SR.GetString(SR.Error_DynamicActivity, activity.QualifiedName, Enum.GetName(typeof(ActivityExecutionStatus), activity.ExecutionStatus)), ErrorNumbers.Error_DynamicActivity); 
            }
            return null;
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
namespace System.Workflow.Activities 
{
    #region Imports

    using System; 
    using System.Text;
    using System.Reflection; 
    using System.Collections; 
    using System.CodeDom;
    using System.Collections.Generic; 
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Drawing.Drawing2D; 
    using System.Drawing.Design;
    using System.Workflow.ComponentModel; 
    using System.Runtime.Serialization; 
    using System.Workflow.ComponentModel.Compiler;
    using System.Workflow.Activities.Common; 

    #endregion

 
    [SRDescription(SR.ConditionalActivityDescription)]
    [ToolboxItem(typeof(IfElseToolboxItem))] 
    [Designer(typeof(IfElseDesigner), typeof(IDesigner))] 
    [SRCategory(SR.Standard)]
    [ToolboxBitmap(typeof(IfElseActivity), "Resources.Decision.png")] 
    [ActivityValidator(typeof(IfElseValidator))]
    public sealed class IfElseActivity: CompositeActivity , IActivityEventListener
    {
 
        #region Constructors
 
        public IfElseActivity() 
        {
        } 

        public IfElseActivity(string name)
            : base(name)
        { 
        }
 
        #endregion 

        public IfElseBranchActivity AddBranch(ICollection activities) 
        {
            if (activities == null)
                throw new ArgumentNullException("activities");
 
            return AddBranch(activities, null);
        } 
 
        public IfElseBranchActivity AddBranch(ICollection activities, ActivityCondition branchCondition)
        { 
            if (activities == null)
                throw new ArgumentNullException("activities");

            if (!this.DesignMode) 
                throw new InvalidOperationException(SR.GetString(SR.Error_ConditionalBranchUpdateAtRuntime));
 
            IfElseBranchActivity branchActivity = new IfElseBranchActivity(); 

            foreach(Activity activity in activities) 
                branchActivity.Activities.Add(activity);

            branchActivity.Condition = branchCondition;
            this.Activities.Add(branchActivity); 

            return branchActivity; 
        } 

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) 
        {
            if (executionContext == null)
                throw new ArgumentNullException("executionContext");
 
            bool noneTrue = true;
 
            for (int i = 0; i < this.EnabledActivities.Count; ++i) 
            {
                IfElseBranchActivity branch = this.EnabledActivities[i] as IfElseBranchActivity; 

                // Else case dont have enable condition so find them and enable it
                if (branch.Condition == null || branch.Condition.Evaluate(branch, executionContext))
                { 
                    noneTrue = false;
                    branch.RegisterForStatusChange(Activity.ClosedEvent, this); 
                    executionContext.ExecuteActivity(branch); 
                    break;
                } 
            }

            return noneTrue ? ActivityExecutionStatus.Closed : ActivityExecutionStatus.Executing;
        } 

        protected override ActivityExecutionStatus Cancel(ActivityExecutionContext executionContext) 
        { 
            bool canCloseNow = true;
 
            for (int i = 0; i < this.EnabledActivities.Count; ++i)
            {
                Activity childBranch = this.EnabledActivities[i];
 
                if (childBranch.ExecutionStatus == ActivityExecutionStatus.Executing)
                { 
                    canCloseNow = false; 
                    executionContext.CancelActivity(childBranch);
                    break; 
                }
                else if (childBranch.ExecutionStatus == ActivityExecutionStatus.Canceling || childBranch.ExecutionStatus == ActivityExecutionStatus.Faulting)
                {
                    canCloseNow = false; 
                    break;
                } 
            } 
            return canCloseNow ? ActivityExecutionStatus.Closed : ActivityExecutionStatus.Canceling;
        } 

        void IActivityEventListener.OnEvent(object sender, ActivityExecutionStatusChangedEventArgs e)
        {
            if (sender == null) 
                throw new ArgumentNullException("sender");
            if (e == null) 
                throw new ArgumentNullException("e"); 

            ActivityExecutionContext context = sender as ActivityExecutionContext; 

            if (context == null)
                throw new ArgumentException(SR.Error_SenderMustBeActivityExecutionContext, "sender");
 
            context.CloseActivity();
        } 
    } 

    internal sealed class IfElseValidator : CompositeActivityValidator 
    {
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            ValidationErrorCollection validationErrors = base.Validate(manager, obj); 

            IfElseActivity ifElse = obj as IfElseActivity; 
            if (ifElse == null) 
                throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(IfElseActivity).FullName), "obj");
 
            // Validate number of children
            if (ifElse.EnabledActivities.Count < 1)
                validationErrors.Add(new ValidationError(SR.GetString(SR.Error_ConditionalLessThanOneChildren), ErrorNumbers.Error_IfElseLessThanOneChildren));
 
            // all child activities must be IfElse branch
            foreach (Activity activity in ifElse.EnabledActivities) 
            { 
                if (!(activity is IfElseBranchActivity))
                { 
                    validationErrors.Add(new ValidationError(SR.GetString(SR.Error_ConditionalDeclNotAllConditionalBranchDecl), ErrorNumbers.Error_IfElseNotAllIfElseBranchDecl));
                    break;
                }
            } 
            return validationErrors;
        } 
 
        public override ValidationError ValidateActivityChange(Activity activity, ActivityChangeAction action)
        { 
            if (activity == null)
                throw new ArgumentNullException("activity");
            if (action == null)
                throw new ArgumentNullException("action"); 

            if (activity.ExecutionStatus != ActivityExecutionStatus.Initialized && 
                 activity.ExecutionStatus != ActivityExecutionStatus.Closed) 
            {
                return new ValidationError(SR.GetString(SR.Error_DynamicActivity, activity.QualifiedName, Enum.GetName(typeof(ActivityExecutionStatus), activity.ExecutionStatus)), ErrorNumbers.Error_DynamicActivity); 
            }
            return null;
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK