Esempio n. 1
0
        public InterceptorWrapperCollection(Type classType, Type proxyType)
        {
            if (classType == null)
            {
                throw new ArgumentNullException(nameof(classType));
            }

            if (proxyType == null)
            {
                throw new ArgumentNullException(nameof(proxyType));
            }

            _wrappers = new Dictionary <int, InterceptorWrapper>();
            SetClassInterceptors(_wrappers, classType);
            var nonInterceptAttribute = typeof(NonInterceptAttribute);

            foreach (var methodHandle in HandleCollection.GetHandles(proxyType.MetadataToken))
            {
                var method = MethodBase.GetMethodFromHandle(methodHandle);
                if (!method.IsDefined(nonInterceptAttribute))
                {
                    var(callingMethodInterceptors, calledMethodInterceptors, exceptionInterceptor) = GetMemberInterceptors(method);
                    _wrappers.Add(method.MetadataToken, new InterceptorWrapper
                    {
                        CallingInterceptors  = new List <ICallingInterceptor>(callingMethodInterceptors),
                        CalledInterceptors   = new List <ICalledInterceptor>(calledMethodInterceptors),
                        ExceptionInterceptor = exceptionInterceptor
                    });
                }
            }
        }
Esempio n. 2
0
        public InterceptorWrapperCollection(Type interfaceType, Type classType, Type proxyType)
        {
            if (interfaceType == null)
            {
                throw new ArgumentNullException(nameof(interfaceType));
            }

            if (classType == null)
            {
                throw new ArgumentNullException(nameof(classType));
            }

            if (proxyType == null)
            {
                throw new ArgumentNullException(nameof(proxyType));
            }

            _wrappers = new Dictionary <int, InterceptorWrapper>();
            SetClassInterceptors(_wrappers, classType);
            if (!interfaceType.IsDefined(typeof(NonInterceptAttribute)))
            {
                var(callingInterceptors, calledInterceptors) = GetMemberInterceptorsWithoutException(interfaceType);
                if (_wrappers.ContainsKey(0))
                {
                    _wrappers[0].CallingInterceptors.AddRange(callingInterceptors);
                    _wrappers[0].CalledInterceptors.AddRange(calledInterceptors);
                    if (_wrappers[0].ExceptionInterceptor == null)
                    {
                        _wrappers[0].ExceptionInterceptor = interfaceType.GetCustomAttribute <ExceptionInterceptAttribute>();
                    }
                }
                else
                {
                    _wrappers.Add(0, new InterceptorWrapper
                    {
                        CallingInterceptors  = new List <ICallingInterceptor>(callingInterceptors),
                        CalledInterceptors   = new List <ICalledInterceptor>(calledInterceptors),
                        ExceptionInterceptor = interfaceType.GetCustomAttribute <ExceptionInterceptAttribute>()
                    });
                }
            }

            var nonInterceptAttribute = typeof(NonInterceptAttribute);
            var interfaceMethods      = interfaceType.GetMethods(
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.DeclaredOnly
                );

            foreach (var methodHandle in HandleCollection.GetHandles(proxyType.MetadataToken))
            {
                var method = MethodBase.GetMethodFromHandle(methodHandle);
                if (!method.IsDefined(nonInterceptAttribute))
                {
                    var(callingMethodInterceptors, calledMethodInterceptors, exceptionMethodInterceptor) = GetMemberInterceptors(method);
                    var wrapper = new InterceptorWrapper
                    {
                        CallingInterceptors  = new List <ICallingInterceptor>(callingMethodInterceptors),
                        CalledInterceptors   = new List <ICalledInterceptor>(calledMethodInterceptors),
                        ExceptionInterceptor = exceptionMethodInterceptor
                    };

                    foreach (var interfaceMethod in interfaceMethods)
                    {
                        if (method.Name == interfaceMethod.Name || method.Name == interfaceType.FullName + "." + interfaceMethod.Name)
                        {
                            var(callingInterfaceMethodInterceptors, calledInterfaceMethodInterceptors) =
                                GetMemberInterceptorsWithoutException(interfaceMethod);
                            wrapper.CallingInterceptors.AddRange(callingInterfaceMethodInterceptors);
                            wrapper.CalledInterceptors.AddRange(calledInterfaceMethodInterceptors);
                            if (wrapper.ExceptionInterceptor == null) // 接口方法的优先级较低
                            {
                                wrapper.ExceptionInterceptor = interfaceMethod.GetCustomAttribute <ExceptionInterceptAttribute>();
                            }

                            break;
                        }
                    }

                    _wrappers.Add(method.MetadataToken, wrapper);
                }
            }
        }