コード例 #1
0
        private bool ValidateInvokeAttribute(RuleValidation validation, MemberInfo member, Type contextType, Stack <MemberInfo> methodStack)
        {
            ValidationError error;

            if (string.IsNullOrEmpty(this.methodInvoked))
            {
                error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, new object[] { member.Name, base.GetType().Name, Messages.NullValue }), 0x56b, true);
                error.UserData["ErrorObject"] = this;
                validation.AddError(error);
                return(false);
            }
            bool flag = true;

            MemberInfo[] infoArray = contextType.GetMember(this.methodInvoked, MemberTypes.Property | MemberTypes.Method, BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
            if ((infoArray == null) || (infoArray.Length == 0))
            {
                error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, new object[] { member.Name, base.GetType().Name, this.methodInvoked }), 0x56b, true);
                error.UserData["ErrorObject"] = this;
                validation.AddError(error);
                return(false);
            }
            for (int i = 0; i < infoArray.Length; i++)
            {
                MemberInfo item = infoArray[i];
                if (!methodStack.Contains(item))
                {
                    methodStack.Push(item);
                    object[] customAttributes = item.GetCustomAttributes(typeof(RuleAttribute), true);
                    if ((customAttributes != null) && (customAttributes.Length != 0))
                    {
                        foreach (RuleAttribute attribute in customAttributes)
                        {
                            RuleReadWriteAttribute attribute2 = attribute as RuleReadWriteAttribute;
                            if (attribute2 != null)
                            {
                                if (attribute2.Target == RuleAttributeTarget.Parameter)
                                {
                                    error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvokeAttrRefersToParameterAttribute, new object[] { item.Name }), 0x1a5, true);
                                    error.UserData["ErrorObject"] = this;
                                    validation.AddError(error);
                                    flag = false;
                                }
                                else
                                {
                                    attribute2.Validate(validation, item, contextType, null);
                                }
                            }
                            else
                            {
                                ((RuleInvokeAttribute)attribute).ValidateInvokeAttribute(validation, item, contextType, methodStack);
                            }
                        }
                    }
                    methodStack.Pop();
                }
            }
            return(flag);
        }
コード例 #2
0
        private bool ValidateInvokeAttribute(RuleValidation validation, MemberInfo member, Type contextType, Stack <MemberInfo> methodStack)
        {
            string          message;
            ValidationError error;

            if (string.IsNullOrEmpty(methodInvoked))
            {
                // Invoked method or property name was null or empty.
                message = string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, member.Name, this.GetType().Name, Messages.NullValue);
                error   = new ValidationError(message, ErrorNumbers.Warning_RuleAttributeNoMatch, true);
                error.UserData[RuleUserDataKeys.ErrorObject] = this;
                validation.AddError(error);
                return(false);
            }

            bool valid = true;

            // Go through all the methods and properties on the target context,
            // looking for all the ones that match the name on the attribute.
            MemberInfo[] members = contextType.GetMember(methodInvoked, MemberTypes.Method | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            if (members == null || members.Length == 0)
            {
                // Invoked method or property didn't exist.
                message = string.Format(CultureInfo.CurrentCulture, Messages.AttributeMethodNotFound, member.Name, this.GetType().Name, methodInvoked);
                error   = new ValidationError(message, ErrorNumbers.Warning_RuleAttributeNoMatch, true);
                error.UserData[RuleUserDataKeys.ErrorObject] = this;
                validation.AddError(error);
                valid = false;
            }
            else
            {
                for (int i = 0; i < members.Length; ++i)
                {
                    MemberInfo mi = members[i];
                    if (!methodStack.Contains(mi)) // Prevent recursion
                    {
                        methodStack.Push(mi);

                        object[] attrs = mi.GetCustomAttributes(typeof(RuleAttribute), true);
                        if (attrs != null && attrs.Length != 0)
                        {
                            foreach (RuleAttribute invokedRuleAttr in attrs)
                            {
                                RuleReadWriteAttribute readWriteAttr = invokedRuleAttr as RuleReadWriteAttribute;
                                if (readWriteAttr != null)
                                {
                                    // This read/write attribute may not specify a target of "Parameter", since
                                    // we can't map from the invoker's parameters to the invokee's parameters.
                                    if (readWriteAttr.Target == RuleAttributeTarget.Parameter)
                                    {
                                        message = string.Format(CultureInfo.CurrentCulture, Messages.InvokeAttrRefersToParameterAttribute, mi.Name);
                                        error   = new ValidationError(message, ErrorNumbers.Error_InvokeAttrRefersToParameterAttribute, true);
                                        error.UserData[RuleUserDataKeys.ErrorObject] = this;
                                        validation.AddError(error);
                                        valid = false;
                                    }
                                    else
                                    {
                                        // Validate the read/write attribute normally.
                                        readWriteAttr.Validate(validation, mi, contextType, null);
                                    }
                                }
                                else
                                {
                                    RuleInvokeAttribute invokeAttr = (RuleInvokeAttribute)invokedRuleAttr;
                                    invokeAttr.ValidateInvokeAttribute(validation, mi, contextType, methodStack);
                                }
                            }
                        }

                        methodStack.Pop();
                    }
                }
            }

            return(valid);
        }