private PointcutSelector CreateAdviceSelector(ReflectionNode node, WeavingContext context)
        {
            var adviceSelector          = new PointcutSelector();
            var excludeAdviceAttributes = node.CustomAttributes.Where(ca => ca.AttributeType.SafeEquivalent(context.ExcludeAdviceAttributeType));

            foreach (var excludeAdviceAttribute in excludeAdviceAttributes)
            {
                var rule = new PointcutSelectorRule();
                // full names wildcards
                if (excludeAdviceAttribute.ConstructorArguments.Count == 1)
                {
                    rule.Names.AddRange(GetStrings(excludeAdviceAttribute.ConstructorArguments[0].Value));
                }

                // then named properties
                foreach (var namedArgument in excludeAdviceAttribute.NamedArguments)
                {
                    // names (which should usually not happen)
                    if (namedArgument.Name == nameof(ExcludeAdvicesAttribute.AdvicesTypes))
                    {
                        rule.Names.AddRange(GetStrings(namedArgument.Value));
                    }
                }
                adviceSelector.ExcludeRules.Add(rule);
            }
            if (node.Parent != null)
            {
                adviceSelector = GetAdviceSelector(node.Parent, context) + adviceSelector;
            }
            return(adviceSelector);
        }
        /// <summary>
        /// Creates the pointcut rules from an advice attribute.
        /// The custom attribute provided has to be either <see cref="ExcludePointcutAttribute"/> or <see cref="IncludePointcutAttribute"/>
        /// If not the return <see cref="PointcutSelector"/> are empty
        /// </summary>
        /// <param name="customAttribute">The custom attribute.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private PointcutSelector CreatePointcutSelector(CustomAttribute customAttribute, WeavingContext context)
        {
            var rules = new PointcutSelector();

            rules.IncludeRules.AddRange(CreatePointcutSelectorRule(customAttribute, context.IncludePointcutAttributeType));
            rules.ExcludeRules.AddRange(CreatePointcutSelectorRule(customAttribute, context.ExcludePointcutAttributeType));
            return(rules);
        }
        /// <summary>
        /// Indicates whether the specified node has to be selected for advice
        /// </summary>
        /// <param name="pointcutSelector">The pointcut selector.</param>
        /// <param name="method">The method.</param>
        /// <returns></returns>
        public static bool Select(this PointcutSelector pointcutSelector, MethodDef method)
        {
            var name            = $"{method.DeclaringType.FullName}.{method.Name}".Replace('/', '+');
            var visibilityScope = ((MethodAttributes)method.Attributes).ToVisibilityScope() | ((TypeAttributes)method.DeclaringType.Attributes).ToVisibilityScope();
            var memberKind      = method.GetMemberKind();

            return(pointcutSelector.Select(name, visibilityScope, memberKind));
        }
        /// <summary>
        /// Creates the pointcut rules for a given advice.
        /// </summary>
        /// <param name="adviceTypeDef">The advice type definition.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private PointcutSelector CreatePointcutSelector(TypeDef adviceTypeDef, WeavingContext context)
        {
            var pointcutSelector = new PointcutSelector();

            foreach (var customAttribute in adviceTypeDef.CustomAttributes)
            {
                pointcutSelector += CreatePointcutSelector(customAttribute, context);
            }
            return(pointcutSelector);
        }
        /// <summary>
        /// Creates the pointcut rules for a given advice.
        /// </summary>
        /// <param name="adviceType">Type of the advice.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private PointcutSelector CreatePointcutSelector(ITypeDefOrRef adviceType, WeavingContext context)
        {
            var adviceTypeDef = TypeResolver.Resolve(adviceType);
            var rules         = new PointcutSelector();

            foreach (var customAttribute in adviceTypeDef.CustomAttributes)
            {
                rules += CreatePointcutSelector(customAttribute, context);
            }
            return(rules);
        }
        /// <summary>
        /// Creates the pointcut rules from an advice attribute.
        /// The custom attribute provided has to be either <see cref="ExcludePointcutAttribute"/> or <see cref="IncludePointcutAttribute"/>
        /// If not the returned <see cref="PointcutSelector"/> is empty
        /// </summary>
        /// <param name="customAttribute">The custom attribute.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private PointcutSelector CreatePointcutSelector(CustomAttribute customAttribute, WeavingContext context)
        {
            var pointcutSelector = new PointcutSelector();

            if (context.IncludePointcutAttributeType != null)
            {
                pointcutSelector.IncludeRules.AddRange(CreatePointcutSelectorRule(customAttribute, context.IncludePointcutAttributeType));
            }
            if (context.ExcludePointcutAttributeType != null)
            {
                pointcutSelector.ExcludeRules.AddRange(CreatePointcutSelectorRule(customAttribute, context.ExcludePointcutAttributeType));
            }
            return(pointcutSelector);
        }
Exemple #7
0
        /// <summary>
        /// Creates the pointcut rules for a given advice.
        /// </summary>
        /// <param name="adviceTypeDef">The advice type definition.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        private PointcutSelector CreatePointcutSelector(TypeDef adviceTypeDef, WeavingContext context)
        {
            var pointcutSelector = new PointcutSelector();

            foreach (var customAttribute in adviceTypeDef.CustomAttributes)
            {
                pointcutSelector += CreatePointcutSelector(customAttribute, context);
            }
            var baseType = adviceTypeDef.BaseType.ResolveTypeDef();

            if (baseType != null)
            {
                pointcutSelector += GetPointcutSelector(baseType, context);
            }
            return(pointcutSelector);
        }
Exemple #8
0
        /// <summary>
        /// Creates a <see cref="PointcutSelector"/> for given advice attribute.
        /// </summary>
        /// <param name="adviceType">Type of the advice.</param>
        /// <returns></returns>
        private static PointcutSelector CreatePointcutSelector(Type adviceType)
        {
            var pointcutSelector = new PointcutSelector();

            foreach (PointcutAttribute pointcutAttribute in adviceType.GetInformationReader().GetCustomAttributes(typeof(PointcutAttribute), true))
            {
                var includeRule = CreatePointcutSelectorRule(typeof(IncludePointcutAttribute), pointcutAttribute);
                if (includeRule != null)
                {
                    pointcutSelector.IncludeRules.Add(includeRule);
                }
                var excludeRule = CreatePointcutSelectorRule(typeof(ExcludePointcutAttribute), pointcutAttribute);
                if (excludeRule != null)
                {
                    pointcutSelector.ExcludeRules.Add(excludeRule);
                }
            }
            return(pointcutSelector);
        }
        private PointcutSelector CreateAdviceSelector(ReflectionNode node, WeavingContext context)
        {
            var adviceSelector = new PointcutSelector();
            // Advices should not advise themselves
            var typeReflectionNode = node as TypeReflectionNode;

            if (typeReflectionNode != null && IsMarker(typeReflectionNode.TypeDefinition, context.AdviceInterfaceType))
            {
                Logging.WriteDebug("Excluding {0} from itself", typeReflectionNode.TypeDefinition.FullName);
                adviceSelector.ExcludeRules.Add(new PointcutSelectorRule(typeReflectionNode.TypeDefinition.FullName));
            }
            if (context.ExcludeAdviceAttributeType != null)
            {
                var excludeAdviceAttributes = node.CustomAttributes.Where(ca => ca.AttributeType.SafeEquivalent(context.ExcludeAdviceAttributeType));
                foreach (var excludeAdviceAttribute in excludeAdviceAttributes)
                {
                    var rule = new PointcutSelectorRule();
                    // full names wildcards
                    if (excludeAdviceAttribute.ConstructorArguments.Count == 1)
                    {
                        rule.Names.AddRange(GetStrings(excludeAdviceAttribute.ConstructorArguments[0].Value));
                    }

                    // then named properties
                    foreach (var namedArgument in excludeAdviceAttribute.NamedArguments)
                    {
                        // names (which should usually not happen)
                        if (namedArgument.Name == nameof(ExcludeAdvicesAttribute.AdvicesTypes))
                        {
                            rule.Names.AddRange(GetStrings(namedArgument.Value));
                        }
                    }
                    adviceSelector.ExcludeRules.Add(rule);
                }
            }
            if (node.Parent != null)
            {
                adviceSelector = GetAdviceSelector(node.Parent, context) + adviceSelector;
            }
            return(adviceSelector);
        }
Exemple #10
0
        /// <summary>
        /// Gets the exclude selector.
        /// </summary>
        /// <param name="methodBase">The method base.</param>
        /// <returns></returns>
        private static PointcutSelector GetAdviceSelector(MethodBase methodBase)
        {
            var pointcutSelector = new PointcutSelector();

            // 1. some special case, avoid type from advising it self
            if (typeof(IAdvice).GetAssignmentReader().IsAssignableFrom(methodBase.DeclaringType))
            {
                pointcutSelector.ExcludeRules.Add(new PointcutSelectorRule(methodBase.DeclaringType.FullName));
            }
            // 2. get from method
            foreach (var methodExclude in methodBase.GetAttributes <ExcludeAdvicesAttribute>())
            {
                pointcutSelector.ExcludeRules.Add(new PointcutSelectorRule(methodExclude.AdvicesTypes));
            }
            // 3. from property, if any
            var propertyInfo = GetPropertyInfo(methodBase);

            if (propertyInfo != null)
            {
                foreach (var propertyExclude in propertyInfo.Item1.GetAttributes <ExcludeAdvicesAttribute>())
                {
                    pointcutSelector.ExcludeRules.Add(new PointcutSelectorRule(propertyExclude.AdvicesTypes));
                }
            }
            // 4. from type and outer types
            for (var type = methodBase.DeclaringType; type != null; type = type.DeclaringType)
            {
                foreach (var typeExclude in type.GetInformationReader().GetAttributes <ExcludeAdvicesAttribute>())
                {
                    pointcutSelector.ExcludeRules.Add(new PointcutSelectorRule(typeExclude.AdvicesTypes));
                }
            }
            // 5. from assembly
            foreach (var assemblyExclude in methodBase.DeclaringType.GetInformationReader().Assembly.GetAttributes <ExcludeAdvicesAttribute>())
            {
                pointcutSelector.ExcludeRules.Add(new PointcutSelectorRule(assemblyExclude.AdvicesTypes));
            }
            return(pointcutSelector);
        }
Exemple #11
0
 private static bool SelectAdvice(AdviceInfo adviceInfo, PointcutSelector rules)
 {
     return(rules.Select(adviceInfo.Advice.GetType().FullName, null, null));
 }
        public static bool Select(this PointcutSelector pointcutSelector, ITypeDefOrRef type)
        {
            var name = type.FullName.Replace('/', '+');

            return(pointcutSelector.Select(name, null, null));
        }
Exemple #13
0
        /// <summary>
        /// Indicates whether the specified node has to be selected for advice
        /// </summary>
        /// <param name="pointcutSelector">The pointcut selector.</param>
        /// <param name="method">The method.</param>
        /// <returns></returns>
        public static bool Select(this PointcutSelector pointcutSelector, MethodDef method)
        {
            var name = $"{method.DeclaringType.FullName}.{method.Name}".Replace('/', '+');

            return(pointcutSelector.Select(name, ((MethodAttributes)method.Attributes).ToMemberAttributes() | ((TypeAttributes)method.DeclaringType.Attributes).ToMemberAttributes()));
        }