示例#1
0
        private static AspectInfo BuildAspectByInterceptorAttribute(Type type)
        {
            AspectInfo aspect = null;

            var adviceTypes = type
                              .GetAttributes <InterceptorAttribute>(true)
                              .Select(p => p.InterceptorType)
                              .Union(type.GetCustomAttributes(true)
                                     .Where(p => typeof(IInterceptor).IsAssignableFrom(p.GetType()))
                                     .Select(p => p.GetType()))
                              .ToArray();

            if (adviceTypes != null && adviceTypes.Length != 0)
            {
                aspect = new AspectInfo {
                    TargetType = new SignleTargetTypeInfo {
                        SingleType = type
                    }
                };

                var pointCut = new PointCutInfo();

                aspect.AddPointCut(pointCut);

                pointCut.Signature = new MethodSignature
                {
                    Deep = 3,
                };

                pointCut.Advices = adviceTypes;
            }

            var pointCuts = (from m in type.GetMethods().Where(m => m.IsPublic || m.IsFamily)
                             let attrs = m.GetAttributes <InterceptorAttribute>(true)
                                         where attrs != null && attrs.Length > 0
                                         select new PointCutInfo
            {
                Advices = attrs.Select(p => p.InterceptorType).ToArray(),
                Signature = new MethodSignature
                {
                    Method = m.Name,
                    ReturnType = m.ReturnType.FullName,
                    Flags = CutPointFlags.Method,
                    Access = AccessFlags.All,
                    Arguments = m.GetParameterTypes().Select(p => p.FullName).ToArray()
                }
            })
                            .Union(from m in type.GetMethods().Where(m => m.IsPublic || m.IsFamily)
                                   let attrs = m.GetCustomAttributes(true).Where(p => typeof(IInterceptor).IsAssignableFrom(p.GetType())).ToArray()
                                               where attrs != null && attrs.Length > 0
                                               select new PointCutInfo
            {
                Advices   = attrs.Select(p => p.GetType()).ToArray(),
                Signature = new MethodSignature
                {
                    Method     = m.Name,
                    ReturnType = m.ReturnType.FullName,
                    Flags      = CutPointFlags.Method,
                    Access     = AccessFlags.All,
                    Arguments  = m.GetParameterTypes().Select(p => p.FullName).ToArray()
                }
            })
                            .ToArray();


            if (pointCuts != null && pointCuts.Length > 0)
            {
                if (aspect == null)
                {
                    aspect = new AspectInfo();
                }

                foreach (var pointCut in pointCuts)
                {
                    aspect.AddPointCut(pointCut);
                }
            }

            return(aspect);
        }