Constraint.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / Validation / Constraint.cs / 1305376 / Constraint.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities.Validation 
{
    using System; 
    using System.Collections.Generic; 
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime; 
    using System.Windows.Markup;
    using System.Collections.ObjectModel;

    public abstract class Constraint : NativeActivity 
    {
        public const string ValidationErrorListPropertyName = "System.Activities.Validation.Constraint.ValidationErrorList"; 
 
        internal const string ToValidateArgumentName = "ToValidate";
        internal const string ValidationErrorListArgumentName = "ViolationList"; 
        internal const string ToValidateContextArgumentName = "ToValidateContext";

        RuntimeArgument toValidate;
        RuntimeArgument violationList; 
        RuntimeArgument toValidateContext;
 
        internal Constraint() 
        {
            this.toValidate = new RuntimeArgument(ToValidateArgumentName, typeof(object), ArgumentDirection.In); 
            this.toValidateContext = new RuntimeArgument(ToValidateContextArgumentName, typeof(ValidationContext), ArgumentDirection.In);
            this.violationList = new RuntimeArgument(ValidationErrorListArgumentName, typeof(IList), ArgumentDirection.Out);
        }
 
        public static void AddValidationError(NativeActivityContext context, ValidationError error)
        { 
            List validationErrorList = context.Properties.Find(ValidationErrorListPropertyName) as List; 

            if (validationErrorList == null) 
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.AddValidationErrorMustBeCalledFromConstraint(typeof(Constraint).Name)));
            }
 
            validationErrorList.Add(error);
        } 
 
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        { 
            metadata.SetArgumentsCollection(
                new Collection
                {
                    this.toValidate, 
                    this.violationList,
                    this.toValidateContext 
                }); 
        }
 
        protected override void Execute(NativeActivityContext context)
        {
            object objectToValidate = this.toValidate.Get(context);
            ValidationContext objectToValidateContext = this.toValidateContext.Get(context); 

            if (objectToValidate == null) 
            { 
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.CannotValidateNullObject(typeof(Constraint).Name, this.DisplayName)));
            } 

            if (objectToValidateContext == null)
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ValidationContextCannotBeNull(typeof(Constraint).Name, this.DisplayName))); 
            }
 
            List validationErrorList = new List(1); 
            context.Properties.Add(ValidationErrorListPropertyName, validationErrorList);
 
            this.violationList.Set(context, validationErrorList);

            OnExecute(context, objectToValidate, objectToValidateContext);
        } 

        [SuppressMessage(FxCop.Category.Naming, FxCop.Rule.IdentifiersShouldNotContainTypeNames, 
            Justification = "Can't replace object with Object because of casing rules")] 
        protected abstract void OnExecute(NativeActivityContext context, object objectToValidate, ValidationContext objectToValidateContext);
    } 

    [ContentProperty("Body")]
    public sealed class Constraint : Constraint
    { 
        public Constraint()
        { 
        } 

        public ActivityAction Body 
        {
            get;
            set;
        } 
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        { 
            base.CacheMetadata(metadata); 

            if (this.Body != null) 
            {
                metadata.SetDelegatesCollection(new Collection { this.Body });
            }
        } 

        protected override void OnExecute(NativeActivityContext context, object objectToValidate, ValidationContext objectToValidateContext) 
        { 
            if (this.Body != null)
            { 
                context.ScheduleAction(this.Body, (T)objectToValidate, objectToValidateContext);
            }
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

                        

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