Пример #1
0
        IEnumerable <AspectInstance> IAspectProvider.ProvideAspects(object targetElement)
        {
            var targetType = (Type)targetElement;

            foreach (var location in targetType.GetFields(BindingFlags.Instance | BindingFlags.Public).Union(
                         targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Cast <MemberInfo>()))
            {
                if (location.IsDefined(typeof(RequiresPermissionBaseAttribute), true))
                {
                    continue;
                }

                var aspect = new LocationAuthorizationAspect();
                aspect.AddPermission(0, permissionFactory);

                yield return(new AspectInstance(location, aspect));
            }
        }
        /// <summary>
        ///   Invoked at build time to get the list of aspects required to implement the current custom attribute.
        /// </summary>
        /// <param name="targetElement">Declaration on which the attribute has been applied.</param>
        /// <returns>A collection of aspect instances.</returns>
        public virtual IEnumerable <AspectInstance> ProvideAspects(object targetElement)
        {
            var aspectRepositoryService =
                PostSharpEnvironment.CurrentProject.GetService <IAspectRepositoryService>();

            var aspects = aspectRepositoryService.GetAspectInstances(targetElement) ?? new IAspectInstance[0];


            var aspect = (AuthorizationAspect)aspects.Select(i => i.Aspect).SingleOrDefault(i => i is AuthorizationAspect);

            if (aspect == null)
            {
                if (targetElement is MethodBase)
                {
                    aspect = new MethodAuthorizationAspect();
                }
                else if (targetElement is PropertyInfo || targetElement is FieldInfo)
                {
                    aspect = new LocationAuthorizationAspect();
                }
                else if (targetElement is ParameterInfo)
                {
                    aspect = new MethodAuthorizationAspect();
                }
                else
                {
                    throw new Exception($"Unexpected aspect target: {targetElement.GetType().Name}");
                }

                yield return(new AspectInstance(targetElement, aspect));
            }

            var parameterIndex = 0;

            if (targetElement is ParameterInfo parameterInfo)
            {
                parameterIndex = parameterInfo.Position + 1;
            }

            aspect.AddPermission(parameterIndex, this);
        }