示例#1
0
 //
 //  Method extracted from OnRuntimeInitialized
 //  used on special Evaluation case too.
 //
 private static RuleDefinitions GetRuleDefinitions(
     Activity activity, out CompositeActivity declaringActivity)
 {
     declaringActivity = Helpers.GetDeclaringActivity(activity);
     if (declaringActivity == null)
     {
         declaringActivity = Helpers.GetRootActivity(activity) as CompositeActivity;
     }
     return(ConditionHelper.Load_Rules_RT(declaringActivity));
 }
示例#2
0
        public static CodeExpression Clone(CodeExpression originalExpression)
        {
            if (originalExpression == null)
            {
                return(null);
            }
            CodeExpression result = GetExpression(originalExpression).Clone(originalExpression);

            ConditionHelper.CloneUserData(originalExpression, result);
            return(result);
        }
示例#3
0
        internal static RuleDefinitions Load_Rules_RT(Activity declaringActivity)
        {
            RuleDefinitions rules = declaringActivity.GetValue(RuleDefinitions.RuleDefinitionsProperty) as RuleDefinitions;

            if (rules == null)
            {
                rules = ConditionHelper.GetRuleDefinitionsFromManifest(declaringActivity.GetType());
                if (rules != null)
                {
                    declaringActivity.SetValue(RuleDefinitions.RuleDefinitionsProperty, rules);
                }
            }
            return(rules);
        }
        public static CodeExpression Clone(CodeExpression originalExpression)
        {
            if (originalExpression == null)
            {
                return(null);
            }

            RuleExpressionInternal ruleExpr = GetExpression(originalExpression);
            CodeExpression         newExpr  = ruleExpr.Clone(originalExpression);

            ConditionHelper.CloneUserData(originalExpression, newExpr);

            return(newExpr);
        }
示例#5
0
        private static CodeMethodReferenceExpression CloneMethodReference(CodeMethodReferenceExpression oldReference)
        {
            CodeMethodReferenceExpression result = new CodeMethodReferenceExpression {
                MethodName   = oldReference.MethodName,
                TargetObject = RuleExpressionWalker.Clone(oldReference.TargetObject)
            };

            foreach (CodeTypeReference reference in oldReference.TypeArguments)
            {
                result.TypeArguments.Add(TypeReferenceExpression.CloneType(reference));
            }
            ConditionHelper.CloneUserData(oldReference, result);
            return(result);
        }
示例#6
0
        internal static CodeTypeReference CloneType(CodeTypeReference oldType)
        {
            if (oldType == null)
            {
                return(null);
            }
            CodeTypeReference result = new CodeTypeReference {
                ArrayElementType = CloneType(oldType.ArrayElementType),
                ArrayRank        = oldType.ArrayRank,
                BaseType         = oldType.BaseType
            };

            foreach (CodeTypeReference reference2 in oldType.TypeArguments)
            {
                result.TypeArguments.Add(CloneType(reference2));
            }
            ConditionHelper.CloneUserData(oldType, result);
            return(result);
        }
 protected override void InitializeProperties()
 {
     lock (this.syncLock)
     {
         if (!this._runtimeInitialized)
         {
             Activity          parentDependencyObject = base.ParentDependencyObject as Activity;
             CompositeActivity declaringActivity      = Helpers.GetDeclaringActivity(parentDependencyObject);
             if (declaringActivity == null)
             {
                 declaringActivity = Helpers.GetRootActivity(parentDependencyObject) as CompositeActivity;
             }
             RuleDefinitions definitions = ConditionHelper.Load_Rules_RT(declaringActivity);
             if (definitions != null)
             {
                 definitions.OnRuntimeInitialized();
             }
             base.InitializeProperties();
             this._runtimeInitialized = true;
         }
     }
 }
示例#8
0
        internal static object OnGetRuleConditions(DependencyObject dependencyObject)
        {
            if (dependencyObject == null)
            {
                throw new ArgumentNullException("dependencyObject");
            }
            RuleDefinitions valueBase = dependencyObject.GetValueBase(RuleDefinitionsProperty) as RuleDefinitions;

            if (valueBase == null)
            {
                Activity activity = dependencyObject as Activity;
                if (activity.Parent == null)
                {
                    valueBase = ConditionHelper.GetRuleDefinitionsFromManifest(activity.GetType());
                    if (valueBase != null)
                    {
                        dependencyObject.SetValue(RuleDefinitionsProperty, valueBase);
                    }
                }
            }
            return(valueBase);
        }
示例#9
0
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            CodeCastExpression expression2 = (CodeCastExpression)expression;

            if (isWritten)
            {
                ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeCastExpression).ToString() }), 0x17a);
                item.UserData["ErrorObject"] = expression2;
                validation.Errors.Add(item);
                return(null);
            }
            if (expression2.Expression == null)
            {
                ValidationError error2 = new ValidationError(Messages.NullCastExpr, 0x53d);
                error2.UserData["ErrorObject"] = expression2;
                validation.Errors.Add(error2);
                return(null);
            }
            if (expression2.Expression is CodeTypeReferenceExpression)
            {
                ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression2.Expression.GetType().FullName }), 0x548);
                error.UserData["ErrorObject"] = expression2.Expression;
                validation.AddError(error);
                return(null);
            }
            if (expression2.TargetType == null)
            {
                ValidationError error4 = new ValidationError(Messages.NullCastType, 0x53d);
                error4.UserData["ErrorObject"] = expression2;
                validation.Errors.Add(error4);
                return(null);
            }
            RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, expression2.Expression, false);

            if (info == null)
            {
                return(null);
            }
            Type expressionType = info.ExpressionType;
            Type type           = validation.ResolveType(expression2.TargetType);

            if (type == null)
            {
                return(null);
            }
            if (expressionType == typeof(NullLiteral))
            {
                if (ConditionHelper.IsNonNullableValueType(type))
                {
                    ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastOfNullInvalid, new object[] { RuleDecompiler.DecompileType(type) }), 0x53d);
                    error5.UserData["ErrorObject"] = expression2;
                    validation.Errors.Add(error5);
                    return(null);
                }
            }
            else
            {
                Type type3 = expressionType;
                if (ConditionHelper.IsNullableValueType(type3))
                {
                    type3 = type3.GetGenericArguments()[0];
                }
                Type type4 = type;
                if (ConditionHelper.IsNullableValueType(type4))
                {
                    type4 = type4.GetGenericArguments()[0];
                }
                bool flag = false;
                if (type3.IsValueType && type4.IsValueType)
                {
                    if (type3.IsEnum)
                    {
                        flag = type4.IsEnum || IsNumeric(type4);
                    }
                    else if (type4.IsEnum)
                    {
                        flag = IsNumeric(type3);
                    }
                    else if (type3 == typeof(char))
                    {
                        flag = IsNumeric(type4);
                    }
                    else if (type4 == typeof(char))
                    {
                        flag = IsNumeric(type3);
                    }
                    else if (type3.IsPrimitive && type4.IsPrimitive)
                    {
                        try
                        {
                            Convert.ChangeType(Activator.CreateInstance(type3), type4, CultureInfo.CurrentCulture);
                            flag = true;
                        }
                        catch (Exception)
                        {
                            flag = false;
                        }
                    }
                }
                if (!flag)
                {
                    ValidationError error6;
                    flag = RuleValidation.ExplicitConversionSpecified(expressionType, type, out error6);
                    if (error6 != null)
                    {
                        error6.UserData["ErrorObject"] = expression2;
                        validation.Errors.Add(error6);
                        return(null);
                    }
                }
                if (!flag)
                {
                    ValidationError error7 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(type) }), 0x53d);
                    error7.UserData["ErrorObject"] = expression2;
                    validation.Errors.Add(error7);
                    return(null);
                }
            }
            return(new RuleExpressionInfo(type));
        }
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            ValidationErrorCollection validationErrors = base.Validate(manager, obj);

            RuleSetReference ruleSetReference = obj as RuleSetReference;

            if (ruleSetReference == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Messages.UnexpectedArgumentType, typeof(RuleSetReference).FullName, "obj");
                throw new ArgumentException(message, "obj");
            }
            Activity activity = manager.Context[typeof(Activity)] as Activity;

            if (activity == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, typeof(Activity).Name);
                throw new InvalidOperationException(message);
            }
            PropertyValidationContext validationContext = manager.Context[typeof(PropertyValidationContext)] as PropertyValidationContext;

            if (validationContext == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, typeof(PropertyValidationContext).Name);
                throw new InvalidOperationException(message);
            }
            if (!string.IsNullOrEmpty(ruleSetReference.RuleSetName))
            {
                RuleDefinitions   rules             = null;
                RuleSetCollection ruleSetCollection = null;

                CompositeActivity declaringActivity = Helpers.GetDeclaringActivity(activity);
                if (declaringActivity == null)
                {
                    declaringActivity = Helpers.GetRootActivity(activity) as CompositeActivity;
                }
                if (activity.Site != null)
                {
                    rules = ConditionHelper.Load_Rules_DT(activity.Site, declaringActivity);
                }
                else
                {
                    rules = ConditionHelper.Load_Rules_RT(declaringActivity);
                }

                if (rules != null)
                {
                    ruleSetCollection = rules.RuleSets;
                }

                if (ruleSetCollection == null || !ruleSetCollection.Contains(ruleSetReference.RuleSetName))
                {
                    string          message         = string.Format(CultureInfo.CurrentCulture, Messages.RuleSetNotFound, ruleSetReference.RuleSetName);
                    ValidationError validationError = new ValidationError(message, ErrorNumbers.Error_RuleSetNotFound);
                    validationError.PropertyName = GetFullPropertyName(manager) + "." + "RuleSetName";
                    validationErrors.Add(validationError);
                }
                else
                {
                    RuleSet actualRuleSet = ruleSetCollection[ruleSetReference.RuleSetName];

                    ITypeProvider typeProvider = (ITypeProvider)manager.GetService(typeof(ITypeProvider));

                    IDisposable localContextScope = (WorkflowCompilationContext.Current == null ? WorkflowCompilationContext.CreateScope(manager) : null);
                    try
                    {
                        RuleValidation validation = new RuleValidation(activity, typeProvider, WorkflowCompilationContext.Current.CheckTypes);
                        actualRuleSet.Validate(validation);
                        ValidationErrorCollection actualRuleSetErrors = validation.Errors;

                        if (actualRuleSetErrors.Count > 0)
                        {
                            string expressionPropertyName = GetFullPropertyName(manager);
                            string genericErrorMsg        = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleSetExpression, expressionPropertyName);
                            int    errorNumber            = ErrorNumbers.Error_InvalidRuleSetExpression;

                            if (activity.Site != null)
                            {
                                foreach (ValidationError actualError in actualRuleSetErrors)
                                {
                                    ValidationError validationError = new ValidationError(actualError.ErrorText, errorNumber);
                                    validationError.PropertyName = expressionPropertyName + "." + "RuleSet Definition";
                                    validationErrors.Add(validationError);
                                }
                            }
                            else
                            {
                                foreach (ValidationError actualError in actualRuleSetErrors)
                                {
                                    ValidationError validationError = new ValidationError(genericErrorMsg + " " + actualError.ErrorText, errorNumber);
                                    validationError.PropertyName = expressionPropertyName;
                                    validationErrors.Add(validationError);
                                }
                            }
                        }
                        //
                    }
                    finally
                    {
                        if (localContextScope != null)
                        {
                            localContextScope.Dispose();
                        }
                    }
                }
            }
            else
            {
                string          message         = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleSetName, "RuleSetReference");
                ValidationError validationError = new ValidationError(message, ErrorNumbers.Error_InvalidRuleSetName);
                validationError.PropertyName = GetFullPropertyName(manager) + "." + "RuleSetName";
                validationErrors.Add(validationError);
            }
            return(validationErrors);
        }
示例#11
0
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }
            if (manager.Context == null)
            {
                throw new InvalidOperationException(Messages.ContextStackMissing);
            }
            ValidationErrorCollection errors    = base.Validate(manager, obj);
            RuleConditionReference    reference = obj as RuleConditionReference;

            if (reference == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Messages.UnexpectedArgumentType, new object[] { typeof(RuleConditionReference).FullName, "obj" }), "obj");
            }
            Activity activity = manager.Context[typeof(Activity)] as Activity;

            if (activity == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, new object[] { typeof(Activity).Name }));
            }
            if (!(manager.Context[typeof(PropertyValidationContext)] is PropertyValidationContext))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, new object[] { typeof(PropertyValidationContext).Name }));
            }
            if (!string.IsNullOrEmpty(reference.ConditionName))
            {
                RuleDefinitions         definitions       = null;
                RuleConditionCollection conditions        = null;
                CompositeActivity       declaringActivity = Helpers.GetDeclaringActivity(activity);
                if (declaringActivity == null)
                {
                    declaringActivity = Helpers.GetRootActivity(activity) as CompositeActivity;
                }
                if (activity.Site != null)
                {
                    definitions = ConditionHelper.Load_Rules_DT(activity.Site, declaringActivity);
                }
                else
                {
                    definitions = ConditionHelper.Load_Rules_RT(declaringActivity);
                }
                if (definitions != null)
                {
                    conditions = definitions.Conditions;
                }
                if ((conditions == null) || !conditions.Contains(reference.ConditionName))
                {
                    ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ConditionNotFound, new object[] { reference.ConditionName }), 0x53e)
                    {
                        PropertyName = base.GetFullPropertyName(manager) + ".ConditionName"
                    };
                    errors.Add(error);
                    return(errors);
                }
                RuleCondition condition = conditions[reference.ConditionName];
                ITypeProvider service   = (ITypeProvider)manager.GetService(typeof(ITypeProvider));
                using ((WorkflowCompilationContext.Current == null) ? WorkflowCompilationContext.CreateScope(manager) : null)
                {
                    RuleValidation validation = new RuleValidation(activity, service, WorkflowCompilationContext.Current.CheckTypes);
                    condition.Validate(validation);
                    ValidationErrorCollection errors2 = validation.Errors;
                    if (errors2.Count > 0)
                    {
                        string fullPropertyName = base.GetFullPropertyName(manager);
                        string errorText        = string.Format(CultureInfo.CurrentCulture, Messages.InvalidConditionExpression, new object[] { fullPropertyName });
                        int    errorNumber      = 0x558;
                        if (activity.Site != null)
                        {
                            ValidationError error2 = new ValidationError(errorText, errorNumber)
                            {
                                PropertyName = fullPropertyName + ".Expression"
                            };
                            errors.Add(error2);
                        }
                        else
                        {
                            foreach (ValidationError error3 in errors2)
                            {
                                ValidationError error4 = new ValidationError(errorText + " " + error3.ErrorText, errorNumber)
                                {
                                    PropertyName = fullPropertyName + ".Expression"
                                };
                                errors.Add(error4);
                            }
                        }
                    }
                    foreach (RuleCondition condition2 in conditions)
                    {
                        if ((condition2.Name == reference.ConditionName) && (condition2 != condition))
                        {
                            ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.DuplicateConditions, new object[] { reference.ConditionName }), 0x53f)
                            {
                                PropertyName = base.GetFullPropertyName(manager) + ".ConditionName"
                            };
                            errors.Add(error5);
                        }
                    }
                    return(errors);
                }
            }
            ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvalidConditionName, new object[] { "ConditionName" }), 0x540)
            {
                PropertyName = base.GetFullPropertyName(manager) + ".ConditionName"
            };

            errors.Add(item);
            return(errors);
        }
示例#12
0
        internal override CodeExpression Clone(CodeExpression expression)
        {
            CodePrimitiveExpression expression2 = (CodePrimitiveExpression)expression;

            return(new CodePrimitiveExpression(ConditionHelper.CloneObject(expression2.Value)));
        }
示例#13
0
        private static bool AdjustValueStandard(Type operandType, object operandValue, Type toType, out object converted)
        {
            // assume it's the same for now
            converted = operandValue;

            // check for null
            if (operandValue == null)
            {
                // are we converting to a value type?
                if (toType.IsValueType)
                {
                    // is the conversion to nullable?
                    if (!ConditionHelper.IsNullableValueType(toType))
                    {
                        // value type and null, so no conversion possible
                        string message = string.Format(CultureInfo.CurrentCulture, Messages.CannotCastNullToValueType, RuleDecompiler.DecompileType(toType));
                        throw new InvalidCastException(message);
                    }

                    // here we have a Nullable<T>
                    // however, we may need to call the implicit conversion operator if the types are not compatible
                    converted = Activator.CreateInstance(toType);
                    return(RuleValidation.StandardImplicitConversion(operandType, toType, null, out ValidationError error));
                }

                // not a value type, so null is valid
                return(true);
            }

            // check simple cases
            Type currentType = operandValue.GetType();

            if (currentType == toType)
            {
                return(true);
            }

            // now the fun begins
            // this should handle most class conversions
            if (toType.IsAssignableFrom(currentType))
            {
                return(true);
            }

            // handle the numerics (both implicit and explicit), along with nullable
            // note that if the value was null, it's already handled, so value cannot be nullable
            if ((currentType.IsValueType) && (toType.IsValueType))
            {
                if (currentType.IsEnum)
                {
                    // strip off the enum representation
                    currentType = Enum.GetUnderlyingType(currentType);
                    ArithmeticLiteral literal = ArithmeticLiteral.MakeLiteral(currentType, operandValue);
                    operandValue = literal.Value;
                }

                bool resultNullable = ConditionHelper.IsNullableValueType(toType);
                Type resultType     = (resultNullable) ? Nullable.GetUnderlyingType(toType) : toType;

                if (resultType.IsEnum)
                {
                    // Enum.ToObject may throw if currentType is not type SByte,
                    // Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64.
                    // So we adjust currentValue to the underlying type (which may throw if out of range)
                    Type underlyingType = Enum.GetUnderlyingType(resultType);
                    if (AdjustValueStandard(currentType, operandValue, underlyingType, out object adjusted))
                    {
                        converted = Enum.ToObject(resultType, adjusted);
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                }
                else if ((resultType.IsPrimitive) || (resultType == typeof(decimal)))
                {
                    // resultType must be a primitive to continue (not a struct)
                    // (enums and generics handled above)
                    if (currentType == typeof(char))
                    {
                        char c = (char)operandValue;
                        if (resultType == typeof(float))
                        {
                            converted = (float)c;
                        }
                        else if (resultType == typeof(double))
                        {
                            converted = (double)c;
                        }
                        else if (resultType == typeof(decimal))
                        {
                            converted = (decimal)c;
                        }
                        else
                        {
                            converted = ((IConvertible)c).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else if (currentType == typeof(float))
                    {
                        float f = (float)operandValue;
                        if (resultType == typeof(char))
                        {
                            converted = (char)f;
                        }
                        else
                        {
                            converted = ((IConvertible)f).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else if (currentType == typeof(double))
                    {
                        double d = (double)operandValue;
                        if (resultType == typeof(char))
                        {
                            converted = (char)d;
                        }
                        else
                        {
                            converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else if (currentType == typeof(decimal))
                    {
                        decimal d = (decimal)operandValue;
                        if (resultType == typeof(char))
                        {
                            converted = (char)d;
                        }
                        else
                        {
                            converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else
                    {
                        if (operandValue is IConvertible convert)
                        {
                            try
                            {
                                converted = convert.ToType(resultType, CultureInfo.CurrentCulture);
                                if (resultNullable)
                                {
                                    converted = Activator.CreateInstance(toType, converted);
                                }
                                return(true);
                            }
                            catch (InvalidCastException)
                            {
                                // not IConvertable, so can't do it
                                return(false);
                            }
                        }
                    }
                }
            }

            // no luck with standard conversions, so no conversion done
            return(false);
        }
示例#14
0
        private static bool AdjustValueStandard(Type operandType, object operandValue, Type toType, out object converted)
        {
            converted = operandValue;
            if (operandValue == null)
            {
                ValidationError error;
                if (!toType.IsValueType)
                {
                    return(true);
                }
                if (!ConditionHelper.IsNullableValueType(toType))
                {
                    throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Messages.CannotCastNullToValueType, new object[] { RuleDecompiler.DecompileType(toType) }));
                }
                converted = Activator.CreateInstance(toType);
                return(RuleValidation.StandardImplicitConversion(operandType, toType, null, out error));
            }
            Type c = operandValue.GetType();

            if (c == toType)
            {
                return(true);
            }
            if (toType.IsAssignableFrom(c))
            {
                return(true);
            }
            if (c.IsValueType && toType.IsValueType)
            {
                if (c.IsEnum)
                {
                    c            = Enum.GetUnderlyingType(c);
                    operandValue = ArithmeticLiteral.MakeLiteral(c, operandValue).Value;
                }
                bool flag     = ConditionHelper.IsNullableValueType(toType);
                Type enumType = flag ? Nullable.GetUnderlyingType(toType) : toType;
                if (enumType.IsEnum)
                {
                    object obj2;
                    Type   underlyingType = Enum.GetUnderlyingType(enumType);
                    if (AdjustValueStandard(c, operandValue, underlyingType, out obj2))
                    {
                        converted = Enum.ToObject(enumType, obj2);
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                }
                else if (enumType.IsPrimitive || (enumType == typeof(decimal)))
                {
                    if (c == typeof(char))
                    {
                        char ch = (char)operandValue;
                        if (enumType == typeof(float))
                        {
                            converted = (float)ch;
                        }
                        else if (enumType == typeof(double))
                        {
                            converted = (double)ch;
                        }
                        else if (enumType == typeof(decimal))
                        {
                            converted = ch;
                        }
                        else
                        {
                            converted = ((IConvertible)ch).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    if (c == typeof(float))
                    {
                        float num = (float)operandValue;
                        if (enumType == typeof(char))
                        {
                            converted = (char)((ushort)num);
                        }
                        else
                        {
                            converted = ((IConvertible)num).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    if (c == typeof(double))
                    {
                        double num2 = (double)operandValue;
                        if (enumType == typeof(char))
                        {
                            converted = (char)((ushort)num2);
                        }
                        else
                        {
                            converted = ((IConvertible)num2).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    if (c == typeof(decimal))
                    {
                        decimal num3 = (decimal)operandValue;
                        if (enumType == typeof(char))
                        {
                            converted = (char)num3;
                        }
                        else
                        {
                            converted = ((IConvertible)num3).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    IConvertible convertible = operandValue as IConvertible;
                    if (convertible != null)
                    {
                        try
                        {
                            converted = convertible.ToType(enumType, CultureInfo.CurrentCulture);
                            if (flag)
                            {
                                converted = Activator.CreateInstance(toType, new object[] { converted });
                            }
                            return(true);
                        }
                        catch (InvalidCastException)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(false);
        }
示例#15
0
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            ValidationErrorCollection errors    = base.Validate(manager, obj);
            RuleSetReference          reference = obj as RuleSetReference;

            if (reference == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Messages.UnexpectedArgumentType, new object[] { typeof(RuleSetReference).FullName, "obj" }), "obj");
            }
            Activity activity = manager.Context[typeof(Activity)] as Activity;

            if (activity == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, new object[] { typeof(Activity).Name }));
            }
            if (!(manager.Context[typeof(PropertyValidationContext)] is PropertyValidationContext))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, new object[] { typeof(PropertyValidationContext).Name }));
            }
            if (!string.IsNullOrEmpty(reference.RuleSetName))
            {
                RuleDefinitions   definitions       = null;
                RuleSetCollection ruleSets          = null;
                CompositeActivity declaringActivity = Helpers.GetDeclaringActivity(activity);
                if (declaringActivity == null)
                {
                    declaringActivity = Helpers.GetRootActivity(activity) as CompositeActivity;
                }
                if (activity.Site != null)
                {
                    definitions = ConditionHelper.Load_Rules_DT(activity.Site, declaringActivity);
                }
                else
                {
                    definitions = ConditionHelper.Load_Rules_RT(declaringActivity);
                }
                if (definitions != null)
                {
                    ruleSets = definitions.RuleSets;
                }
                if ((ruleSets == null) || !ruleSets.Contains(reference.RuleSetName))
                {
                    ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.RuleSetNotFound, new object[] { reference.RuleSetName }), 0x576)
                    {
                        PropertyName = base.GetFullPropertyName(manager) + ".RuleSetName"
                    };
                    errors.Add(error);
                    return(errors);
                }
                RuleSet       set     = ruleSets[reference.RuleSetName];
                ITypeProvider service = (ITypeProvider)manager.GetService(typeof(ITypeProvider));
                using ((WorkflowCompilationContext.Current == null) ? WorkflowCompilationContext.CreateScope(manager) : null)
                {
                    RuleValidation validation = new RuleValidation(activity, service, WorkflowCompilationContext.Current.CheckTypes);
                    set.Validate(validation);
                    ValidationErrorCollection errors2 = validation.Errors;
                    if (errors2.Count > 0)
                    {
                        string fullPropertyName = base.GetFullPropertyName(manager);
                        string str6             = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleSetExpression, new object[] { fullPropertyName });
                        int    errorNumber      = 0x577;
                        if (activity.Site != null)
                        {
                            foreach (ValidationError error2 in errors2)
                            {
                                ValidationError error3 = new ValidationError(error2.ErrorText, errorNumber)
                                {
                                    PropertyName = fullPropertyName + ".RuleSet Definition"
                                };
                                errors.Add(error3);
                            }
                            return(errors);
                        }
                        foreach (ValidationError error4 in errors2)
                        {
                            ValidationError error5 = new ValidationError(str6 + " " + error4.ErrorText, errorNumber)
                            {
                                PropertyName = fullPropertyName
                            };
                            errors.Add(error5);
                        }
                    }
                    return(errors);
                }
            }
            ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleSetName, new object[] { "RuleSetReference" }), 0x578)
            {
                PropertyName = base.GetFullPropertyName(manager) + ".RuleSetName"
            };

            errors.Add(item);
            return(errors);
        }
        public EnumOperationMethodInfo(Type lhs, CodeBinaryOperatorType operation, Type rhs, bool isZero)
        {
            this.op = operation;
            this.expectedParameters = new ParameterInfo[] { new SimpleParameterInfo(lhs), new SimpleParameterInfo(rhs) };
            bool flag  = ConditionHelper.IsNullableValueType(lhs);
            bool flag2 = ConditionHelper.IsNullableValueType(rhs);

            this.lhsBaseType = flag ? Nullable.GetUnderlyingType(lhs) : lhs;
            this.rhsBaseType = flag2 ? Nullable.GetUnderlyingType(rhs) : rhs;
            if (this.lhsBaseType.IsEnum)
            {
                this.lhsRootType = EnumHelper.GetUnderlyingType(this.lhsBaseType);
            }
            else
            {
                this.lhsRootType = this.lhsBaseType;
            }
            if (this.rhsBaseType.IsEnum)
            {
                this.rhsRootType = EnumHelper.GetUnderlyingType(this.rhsBaseType);
            }
            else
            {
                this.rhsRootType = this.rhsBaseType;
            }
            switch (this.op)
            {
            case CodeBinaryOperatorType.Add:
                if (!this.lhsBaseType.IsEnum || !rhs.IsEnum)
                {
                    if (this.lhsBaseType.IsEnum)
                    {
                        this.resultBaseType = this.lhsBaseType;
                    }
                    else
                    {
                        this.resultBaseType = this.rhsBaseType;
                    }
                    break;
                }
                this.resultBaseType = this.lhsRootType;
                break;

            case CodeBinaryOperatorType.Subtract:
                if (!this.rhsBaseType.IsEnum || !this.lhsBaseType.IsEnum)
                {
                    if (this.lhsBaseType.IsEnum)
                    {
                        this.resultRootType = this.lhsRootType;
                        if (isZero && (this.rhsBaseType != this.lhsRootType))
                        {
                            this.resultBaseType = this.lhsRootType;
                        }
                        else
                        {
                            this.resultBaseType = this.lhsBaseType;
                        }
                    }
                    else
                    {
                        this.resultRootType = this.rhsRootType;
                        if (isZero)
                        {
                            this.resultBaseType = this.rhsRootType;
                        }
                        else
                        {
                            this.resultBaseType = this.rhsBaseType;
                        }
                    }
                }
                else
                {
                    this.resultRootType = this.rhsRootType;
                    this.resultBaseType = this.rhsRootType;
                }
                this.resultIsNullable = flag || flag2;
                this.resultType       = this.resultIsNullable ? typeof(Nullable <>).MakeGenericType(new Type[] { this.resultBaseType }) : this.resultBaseType;
                return;

            case CodeBinaryOperatorType.ValueEquality:
            case CodeBinaryOperatorType.LessThan:
            case CodeBinaryOperatorType.LessThanOrEqual:
            case CodeBinaryOperatorType.GreaterThan:
            case CodeBinaryOperatorType.GreaterThanOrEqual:
                this.resultType = typeof(bool);
                return;

            case CodeBinaryOperatorType.BitwiseOr:
            case CodeBinaryOperatorType.BitwiseAnd:
            case CodeBinaryOperatorType.BooleanOr:
            case CodeBinaryOperatorType.BooleanAnd:
                return;

            default:
                return;
            }
            this.resultIsNullable = flag || flag2;
            this.resultType       = this.resultIsNullable ? typeof(Nullable <>).MakeGenericType(new Type[] { this.resultBaseType }) : this.resultBaseType;
        }