private static BlockExpression Compile(RawUriTemplate raw, Expression value)
        {
            var context = new Context(
                target: value,
                statements: new List <Expression>
            {
                Expression.Assign(QueryBuilder, Expression.New(ConstructorOfUrlBuilder, raw.LengthEstimateConstant))
            },
                variables: new List <ParameterExpression>
            {
                QueryBuilder
            });

            for (var i = 0; i < raw.Path.Count; i++)
            {
                AppendToken(context, raw.Path[i]);
            }

            for (var i = 0; i < raw.Query.Count; i++)
            {
                AppendQuery(context, raw.Query[i]);
            }

            context.Add(Expression.Call(QueryBuilder, MethodofUrlBuilder_ToString));
            BlockExpression body = Expression.Block(context.Variables, context.Statements);

            return(body);
        }
#pragma warning restore IDE1006 // Naming Styles

        public static Func <object, string> Compile(RawUriTemplate raw, RuntimeTypeHandle typeHandle)
        {
            ParameterExpression param = Expression.Parameter(typeof(object), "value");
            BlockExpression     body  = Compile(raw, Expression.Convert(param, Type.GetTypeFromHandle(typeHandle)));
            var lambda = Expression.Lambda <Func <object, string> >(body, param);

            return(lambda.Compile());
        }
        public static Func <T, string> Compile <T>(RawUriTemplate raw)
        {
            ParameterExpression param = Expression.Parameter(typeof(T), "value");
            BlockExpression     body  = Compile(raw, param);
            var lambda = Expression.Lambda <Func <T, string> >(body, param);

            return(lambda.Compile());
        }
示例#4
0
 public UrlTemplate(string template)
 {
     Template  = template ?? throw new ArgumentNullException(nameof(template));
     _template = RawUriTemplate.Parse(template);
     _compiled = new ConcurrentDictionary <RuntimeTypeHandle, Func <object, string> >();
 }
示例#5
0
 public UrlTemplate(string template)
 {
     Template  = template ?? throw new ArgumentNullException(nameof(template));
     _compiled = TemplateCompiler.Compile <T>(RawUriTemplate.Parse(template));
 }