Ejemplo n.º 1
0
        private Tuple <Predicate <FieldInfo>, IPointcut> GetTuple(ParameterInfo parameter)
        {
            Predicate <FieldInfo> mapping  = info => false;
            IPointcut             pointcut = new TruePointcut();

            var parameterType = parameter.ParameterType;

            if (parameterType == typeof(IInvocation))
            {
                return(Tuple.Create(mapping, pointcut));
            }
            if (parameterType == typeof(IContext))
            {
                return(Tuple.Create(mapping, pointcut));
            }

            var attributes = parameter.GetCustomAttributes(typeof(StrongContextAttributeBase), true).ToList();

            Assertion.IsTrue(attributes.Count == 1, "must use exactly one strong context attribute");

            var attribute = attributes.Single();

            Assertion.IsTrue(parameterType.IsByRef);
            parameterType = parameterType.GetElementType();

            if (attribute is InstanceAttribute)
            {
                mapping  = info => info.Name == "TypedInstance";
                pointcut = new TypePointcut(parameterType);
                return(Tuple.Create(mapping, pointcut));
            }

            if (attribute is ReturnValueAttribute)
            {
                mapping  = info => info.Name == "TypedReturnValue";
                pointcut = new ReturnTypePointcut(parameterType);
                return(Tuple.Create(mapping, pointcut));
            }

            var parameterAttribute = (ParameterAttribute)attribute;

            if (parameterAttribute.Index != -1)
            {
                mapping  = info => info.Name == "Arg" + parameterAttribute.Index;
                pointcut = new ArgumentIndexPointcut(parameterType, parameterAttribute.Index);
                return(Tuple.Create(mapping, pointcut));
            }

            if (!string.IsNullOrEmpty(parameterAttribute.Name))
            {
                throw new NotImplementedException();
                //pointcuts.Add (new ArgumentNamePointcut (parameter.ParameterType, parameterAttribute.Name));
                //mappings.Add (info => info.Name == parameterAttribute.)
            }

            mapping  = info => parameterType.IsAssignableFrom(info.FieldType);
            pointcut = new ArgumentPointcut(parameterType);

            return(Tuple.Create(mapping, pointcut));
        }
Ejemplo n.º 2
0
        public bool VisitArgumentIndex(ArgumentIndexPointcut pointcut, JoinPoint joinPoint)
        {
            var parameters = joinPoint.Method.GetParameters();

            return(parameters.Length <= pointcut.Index && parameters[pointcut.Index].ParameterType == pointcut.ArgumentType);
        }