Example #1
0
 public static bool ContainsMethod(this MethodInfo[] methods, MethodInfo key)
 {
     if (methods.Length == 0) return false;
     return methods.Select(
         (x) =>
         {
             return x.GetSignature().Equal(key.GetSignature()) &
                 x.Name == key.Name;
         }).Aggregate(
         (a, b) => { return a | b; });
 }
        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;
        }
Example #3
0
 /// <summary>
 /// Adds an interception reference to the specified contract and the method.
 /// </summary>
 /// <param name="contractType">The type of the contract.</param>
 /// <param name="method">The method.</param>
 /// <param name="name">The name of the interception reference.</param>
 public void AddInterceptorRef(Type contractType, MethodInfo method, string name)
 {
     if (this.config.Interception != null)
     {
         if (this.config.Interception.Contracts != null)
         {
             foreach (InterceptContractElement interceptContract in this.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;
                             }
                         }
                         InterceptMethodElement 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();
                         InterceptMethodElement interceptMethodAdd = new InterceptMethodElement();
                         interceptMethodAdd.Signature = method.GetSignature();
                         interceptMethodAdd.InterceptorRefs = new InterceptorRefElementCollection();
                         interceptMethodAdd.InterceptorRefs.Add(new InterceptorRefElement { Name = name });
                         interceptContract.Methods.Add(interceptMethodAdd);
                     }
                     return;
                 }
             }
             InterceptContractElement interceptContractAdd = new InterceptContractElement();
             interceptContractAdd.Type = contractType.AssemblyQualifiedName;
             interceptContractAdd.Methods = new InterceptMethodElementCollection();
             InterceptMethodElement interceptMethodAddToContract = new InterceptMethodElement();
             interceptMethodAddToContract.Signature = method.GetSignature();
             interceptMethodAddToContract.InterceptorRefs = new InterceptorRefElementCollection();
             interceptMethodAddToContract.InterceptorRefs.Add(new InterceptorRefElement { Name = name });
             interceptContractAdd.Methods.Add(interceptMethodAddToContract);
             this.config.Interception.Contracts.Add(interceptContractAdd);
         }
         else
         {
             this.config.Interception.Contracts = new InterceptContractElementCollection();
             InterceptContractElement interceptContractAdd = new InterceptContractElement();
             interceptContractAdd.Type = contractType.AssemblyQualifiedName;
             interceptContractAdd.Methods = new InterceptMethodElementCollection();
             InterceptMethodElement interceptMethodAddToContract = new InterceptMethodElement();
             interceptMethodAddToContract.Signature = method.GetSignature();
             interceptMethodAddToContract.InterceptorRefs = new InterceptorRefElementCollection();
             interceptMethodAddToContract.InterceptorRefs.Add(new InterceptorRefElement { Name = name });
             interceptContractAdd.Methods.Add(interceptMethodAddToContract);
             this.config.Interception.Contracts.Add(interceptContractAdd);
         }
     }
     else
     {
         this.config.Interception = new InterceptionElement();
         this.config.Interception.Contracts = new InterceptContractElementCollection();
         InterceptContractElement interceptContractAdd = new InterceptContractElement();
         interceptContractAdd.Type = contractType.AssemblyQualifiedName;
         interceptContractAdd.Methods = new InterceptMethodElementCollection();
         InterceptMethodElement interceptMethodAddToContract = new InterceptMethodElement();
         interceptMethodAddToContract.Signature = method.GetSignature();
         interceptMethodAddToContract.InterceptorRefs = new InterceptorRefElementCollection();
         interceptMethodAddToContract.InterceptorRefs.Add(new InterceptorRefElement { Name = name });
         interceptContractAdd.Methods.Add(interceptMethodAddToContract);
         this.config.Interception.Contracts.Add(interceptContractAdd);
     }
 }
Example #4
0
        private static void onRegistered(MethodInfo method, RunAtAttribute runAt, int index)
        {
            if (method.IsConstructor) ThrowHelper.Throw("{0} cannot be called @sever since it is constructor.", method.GetSignature());
            if (method.IsVirtual) ThrowHelper.Throw("{0} cannot be called @sever since it is virtual.", method.GetSignature());
            sb.Append("case ").Append(index).Append(":try{");
            if (!method.IsStatic || method.GetParameters().Length > 0) {
                sb.Append("args=Serializer.Deserialize(context");
                if (!method.IsStatic)
                    sb.Append(",typeof(").Append(Name(method.DeclaringType)).Append(')');
                foreach (var param in method.GetParameters()) {
                    sb.Append(",typeof(").Append(Name(param.ParameterType)).Append(')');
                }
                sb.Append(");");
            }
            if (method.ReturnParameter.ParameterType != typeof(void))
                sb.Append("context.Response.Write(\"(\"+Serializer.Serialize(");
            if (method.IsSpecialName) {
                if (method.Name.StartsWith("get_")) {
                    appendThisOrType(method);
                    sb.Append('.').Append(method.Name.Substring(4));
                } else if (method.Name.StartsWith("set_")) {
                    appendThisOrType(method);
                    sb.Append('.').Append(method.Name.Substring(4)).Append("=args[");
                    sb.Append(method.IsStatic ? 0 : 1);
                    sb.Append("]");
                } else {
                    ThrowHelper.Throw("Special name method {0} cannot be called @server.", method.GetSignature());
                }
            } else {
                appendThisOrType(method);
                sb.Append('.').Append(method.Name).Append('(');
                var parameters = method.GetParameters();
                if (parameters.Length > 0) {
                    for (int j = 0; j < parameters.Length; ++j) {
                        sb.Append('(').Append(Name(parameters[j].ParameterType)).Append(")args[").Append(method.IsStatic ? j : (j + 1)).Append("],");
                    }
                    --sb.Length;
                }
                sb.Append(")");
            }
            if (method.ReturnParameter.ParameterType != typeof(void)) {
                sb.Append(")+\")\")");
            } else {
                sb.Append(";context.Response.Write(\"0\")");
            }
            sb.Append(";}catch(Exception e){context.Response.Write(");

            if (runAt.HideExceptionMessage) {
                sb.Append("\"throw Error()\"");
            } else {
                sb.Append("\"throw Error(\"+Serializer.Serialize(e.Message)+\")\"");
            }
            sb.Append(");}break;");
        }