private InterceptMethodElement FindInterceptMethodElement(InterceptContractElement interceptContractElement, MethodInfo method) { if (interceptContractElement == null) return null; if (interceptContractElement.Methods == null) return null; foreach (InterceptMethodElement interceptMethodElement in interceptContractElement.Methods) { string methodSignature = null; if (method.IsGenericMethod) methodSignature = method.GetGenericMethodDefinition().GetSignature(); else methodSignature = method.GetSignature(); if (interceptMethodElement.Signature.Equals(methodSignature)) return interceptMethodElement; } return null; }
/// <summary> /// 为制定类型的指定方法添加拦截器 /// </summary> /// <param name="contractType">需要拦截方法的类型</param> /// <param name="method">需要拦截的方法</param> /// <param name="name">拦截器的名字</param> public void AddInterceptorRef(Type contractType, MethodInfo method, string name) { if (config.Interception != null) { if (config.Interception.Contracts != null) { foreach (InterceptContractElement interceptContract in config.Interception.Contracts) { if (interceptContract.Type.Equals(contractType.AssemblyQualifiedName)) { if (interceptContract.Methods != null) { foreach (InterceptMethodElement interceptMethod in interceptContract.Methods) { if (interceptMethod.Signature.Equals(method.GetSignature())) { if (interceptMethod.InterceptorRefs != null) { foreach ( InterceptorRefElement interceptorRef in interceptMethod.InterceptorRefs) { if (interceptorRef.Name.Equals(name)) return; } interceptMethod.InterceptorRefs.Add(new InterceptorRefElement {Name = name}); } else { interceptMethod.InterceptorRefs = new InterceptorRefElementCollection(); interceptMethod.InterceptorRefs.Add(new InterceptorRefElement {Name = name}); } return; } } var interceptMethodAdd = new InterceptMethodElement(); interceptMethodAdd.Signature = method.GetSignature(); interceptMethodAdd.InterceptorRefs = new InterceptorRefElementCollection(); interceptMethodAdd.InterceptorRefs.Add(new InterceptorRefElement {Name = name}); interceptContract.Methods.Add(interceptMethodAdd); } else { interceptContract.Methods = new InterceptMethodElementCollection(); var interceptMethodAdd = new InterceptMethodElement(); interceptMethodAdd.Signature = method.GetSignature(); interceptMethodAdd.InterceptorRefs = new InterceptorRefElementCollection(); interceptMethodAdd.InterceptorRefs.Add(new InterceptorRefElement {Name = name}); interceptContract.Methods.Add(interceptMethodAdd); } return; } } var interceptContractAdd = new InterceptContractElement(); interceptContractAdd.Type = contractType.AssemblyQualifiedName; interceptContractAdd.Methods = new InterceptMethodElementCollection(); var interceptMethodAddToContract = new InterceptMethodElement(); interceptMethodAddToContract.Signature = method.GetSignature(); interceptMethodAddToContract.InterceptorRefs = new InterceptorRefElementCollection(); interceptMethodAddToContract.InterceptorRefs.Add(new InterceptorRefElement {Name = name}); interceptContractAdd.Methods.Add(interceptMethodAddToContract); config.Interception.Contracts.Add(interceptContractAdd); } else { config.Interception.Contracts = new InterceptContractElementCollection(); var interceptContractAdd = new InterceptContractElement(); interceptContractAdd.Type = contractType.AssemblyQualifiedName; interceptContractAdd.Methods = new InterceptMethodElementCollection(); var interceptMethodAddToContract = new InterceptMethodElement(); interceptMethodAddToContract.Signature = method.GetSignature(); interceptMethodAddToContract.InterceptorRefs = new InterceptorRefElementCollection(); interceptMethodAddToContract.InterceptorRefs.Add(new InterceptorRefElement {Name = name}); interceptContractAdd.Methods.Add(interceptMethodAddToContract); config.Interception.Contracts.Add(interceptContractAdd); } } else { config.Interception = new InterceptionElement(); config.Interception.Contracts = new InterceptContractElementCollection(); var interceptContractAdd = new InterceptContractElement(); interceptContractAdd.Type = contractType.AssemblyQualifiedName; interceptContractAdd.Methods = new InterceptMethodElementCollection(); var interceptMethodAddToContract = new InterceptMethodElement(); interceptMethodAddToContract.Signature = method.GetSignature(); interceptMethodAddToContract.InterceptorRefs = new InterceptorRefElementCollection(); interceptMethodAddToContract.InterceptorRefs.Add(new InterceptorRefElement {Name = name}); interceptContractAdd.Methods.Add(interceptMethodAddToContract); config.Interception.Contracts.Add(interceptContractAdd); } }