コード例 #1
0
        private RawUriTemplate(IReadOnlyList <UriToken> path, IReadOnlyList <UriQuery> query)
        {
            Path           = path;
            Query          = query;
            LengthEstimate = TokenHeuristic;

            for (var i = 0; i < path.Count; i++)
            {
                LengthEstimate += path[i].Type == UriTokenType.Literal ? path[i].Default?.Length ?? TokenHeuristic : TokenHeuristic;
            }

            for (var i = 0; i < query.Count; i++)
            {
                UriQuery q = query[i];

                LengthEstimate += 2; // & =
                for (var j = 0; j < q.Name.Count; j++)
                {
                    LengthEstimate += q.Name[j].Type == UriTokenType.Literal ? q.Name[j].Default?.Length ?? TokenHeuristic : TokenHeuristic;
                }
                for (var j = 0; j < q.Value.Count; j++)
                {
                    LengthEstimate += q.Value[j].Type == UriTokenType.Literal ? q.Value[j].Default?.Length ?? TokenHeuristic : TokenHeuristic;
                }
            }

            LengthEstimateConstant = Expression.Constant(LengthEstimate);
        }
コード例 #2
0
        private static void AppendQuery(Context context, UriQuery query)
        {
            if (query.Value.Count == 1 && query.Value[0].Type != UriTokenType.Literal)
            {
                UriToken         value    = query.Value[0];
                MemberExpression prop     = Expression.PropertyOrField(context.Target, value.Name);
                Type             propType = Nullable(prop.Type);

                if (value.Type == UriTokenType.Collection)
                {
                    MethodInfo getEnumerator = Enumerator(propType);
                    if (getEnumerator != null)
                    {
                        AppendCollection(context, prop, getEnumerator, query.Name, value);
                    }
                    else
                    {
                        throw new InvalidOperationException(Invariant($"An enumerable was expected for {value.Name}."));
                    }
                    return;
                }

                if (value.SubName != null)
                {
                    if (propType.IsEnum)
                    {
                        AppendEnum(context, prop, query.Name, value);
                    }
                    else
                    {
                        throw new InvalidOperationException(Invariant($"An enum was expected for {value.Name}."));
                    }
                    return;
                }

                if (value.Default == null)
                {
                    AppendNullable(context, prop, query.Name, value);
                    return;
                }
            }

            context.Add(Expression.Call(QueryBuilder, MethodofUrlBuilder_StartParameter));

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

            context.Add(Expression.Call(QueryBuilder, MethodofUrlBuilder_StartValue));
            for (var i = 0; i < query.Value.Count; i++)
            {
                AppendToken(context, query.Value[i]);
            }
        }