Example #1
0
        private static AdviceParameterFlag ValidateParameters(MethodInfo advice, AdviceParameterFlag flag)
        {
            var parameters     = advice.GetParameters();
            var parameterCount = parameters.Length;

            if (parameterCount == 0)
            {
                return(AdviceParameterFlag.None);
            }

            var result  = parameters[0].Validate(flag);
            var current = result;

            for (var i = 1; i < parameterCount; i++)
            {
                var temp = parameters[i].Validate(flag);
                if (temp <= current)
                {
                    throw new ArgumentException($"Wrong order or redundant type of parameter {parameters[i].Name} for advice");
                }

                current = temp;
                result |= current;
            }

            return(result);
        }
Example #2
0
        private static AdviceParameterFlag Validate(this ParameterInfo info, AdviceParameterFlag flag)
        {
            var typeFullName = info.ParameterType.FullName;

            if (typeFullName.Equals(MethodExecutionContextTypeName))
            {
                if (!flag.Contains(AdviceParameterFlag.Context))
                {
                    throw new ArgumentException($"Parameter type {typeFullName} is not allowed for this advice");
                }

                return(AdviceParameterFlag.Context);
            }

            if (typeFullName.Equals(MethodExecutionTypeName))
            {
                if (!flag.Contains(AdviceParameterFlag.Execution))
                {
                    throw new ArgumentException($"Parameter type {typeFullName} is not allowed for this advice");
                }

                return(AdviceParameterFlag.Execution);
            }

            if (typeFullName.Equals(ExceptionTypeName))
            {
                if (!flag.Contains(AdviceParameterFlag.Exception))
                {
                    throw new ArgumentException($"Parameter type {typeFullName} is not allowed for this advice");
                }

                return(AdviceParameterFlag.Exception);
            }

            if (typeFullName.Equals(ReturnTypeName))
            {
                if (!flag.Contains(AdviceParameterFlag.Return))
                {
                    throw new ArgumentException($"Parameter type {typeFullName} is not allowed for this advice");
                }

                return(AdviceParameterFlag.Return);
            }

            if (typeFullName.Equals(BooTypeName))
            {
                if (!flag.Contains(AdviceParameterFlag.HasException))
                {
                    throw new ArgumentException($"Parameter type {typeFullName} is not allowed for this advice");
                }

                return(AdviceParameterFlag.HasException);
            }

            throw new ArgumentException($"Parameter type {typeFullName} is not allowed");
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AdviceInfo"/> class.
        /// </summary>
        /// <param name="advice">Advice method.</param>
        /// <param name="aspectName">Name of the aspect, also serves as key of aspect.</param>
        /// <param name="parameterFlag">Flag of parameters of this advice method.</param>
        /// <param name="isSwitchedOn">Default switch status for this aspect.</param>
        public AdviceInfo(MethodInfo advice, string aspectName, AdviceParameterFlag parameterFlag, bool?isSwitchedOn)
        {
#if DEBUG
            if (string.IsNullOrWhiteSpace(aspectName))
            {
                throw new ArgumentNullException("aspectName");
            }
#endif
            Advice        = advice;
            AspectName    = aspectName;
            ParameterFlag = parameterFlag;
            IsSwitchedOn  = isSwitchedOn;
        }
Example #4
0
 /// <summary>
 /// Initializes a new instance of of <see cref="IAdviceInfo"/>.
 /// </summary>
 /// <param name="method">The advice method.</param>
 /// <param name="aspectName">Name of the aspect, also used as key of aspect.</param>
 /// <param name="parameterFlag">Flag of advice method parameters.</param>
 /// <param name="isSwitchedOn">Default switch status.</param>
 /// <returns>The <see cref="IAdviceInfo"/> initialized.</returns>
 internal static IAdviceInfo InitializeAdviceInfo(MethodInfo method, string aspectName, AdviceParameterFlag parameterFlag, bool?isSwitchedOn) => new AdviceInfo(method, aspectName, parameterFlag, isSwitchedOn);
Example #5
0
 /// <summary>
 /// Checks if the given advice parameter flag contains all contents of the other.
 /// </summary>
 /// <param name="self">The given advice parameter flag.</param>
 /// <param name="flag">The advice parameter flag to check against.</param>
 /// <returns>True if given advice parameter flag does contain all content of the other, false elsewise.</returns>
 public static bool Contains(this AdviceParameterFlag self, AdviceParameterFlag flag) => (self & flag) == flag;