示例#1
0
 static Expression<Func<Widget, bool>> GetNewExpression(bool withParamName)
 {
     Expression<Func<Thing, bool>> expr = t => t.Stuff.StartsWith("anything");
     var widgetParam = withParamName ? Expression.Parameter(typeof(Widget), "w") : Expression.Parameter(typeof(Widget));
     var widgetDotThingExpr = Expression.MakeMemberAccess(widgetParam, typeof(Widget).GetTypeInfo().GetDeclaredProperty(nameof(Widget.Thing)));
     var newBody = new ParamReplacer().Replace(expr.Parameters[0], widgetDotThingExpr).Visit(expr.Body);
     return Expression.Lambda<Func<Widget, bool>>(newBody, widgetParam);
 }
示例#2
0
 public bool TryMatch(RouteMetadata route, HttpListenerContext context, ParamReplacer paramReplacer)
 {
     try
     {
         if (context.Request.HttpMethod.Equals(route.HttpMethod, StringComparison.OrdinalIgnoreCase))
         {
             string pattern = paramReplacer.ReplacePattern(route.Pattern);
             bool   isPost  = context.Request.HttpMethod.Equals("post", StringComparison.OrdinalIgnoreCase);
             int    argLen  = route.MethodParams.Length - (isPost ? 1 : 0);
             var    match   = Regex.Match(context.Request.Url.LocalPath, $"^{pattern}$");
             if (match.Success && argLen == match.Groups.Count - 1)
             {
                 Parameters = new object[route.MethodParams.Length];
                 for (int i = 0; i < argLen; i++)
                 {
                     var param = route.MethodParams[i];
                     Parameters[i] = Convert.ChangeType(match.Groups[param.Name].Value, param.ParameterType);
                 }
                 if (isPost)
                 {
                     try
                     {
                         Parameters[argLen] = route.BodyTransformer.Transform(route.MethodParams[argLen].ParameterType, context.Request.InputStream);
                     }
                     catch (Exception e)
                     {
                         FatalError = e;
                     }
                 }
                 return(Success = true);
             }
         }
     }
     catch (Exception) { }
     return(Success = false);
 }