public static Expression?ConstantToEntity(Expression expression) { ConstantExpression?c = expression as ConstantExpression; if (c == null) { return(null); } if (c.Value == null) { return(c); } if (c.Type.IsIEntity()) { var ei = (Entity)c.Value; var id = GetPrimaryKeyValue(ei.IdOrNull, ei.GetType()); return(new EntityExpression(ei.GetType(), new PrimaryKeyExpression(id), null, null, null, null, null, avoidExpandOnRetrieving: true)); } return(null); }
protected override Expression VisitConstant(ConstantExpression constant) { ConstantExpression?candidate = MakeCandidateMatch(constant); CheckEqual(constant.Value, candidate?.Value); return(base.VisitConstant(constant)); }
internal MethodStub(Func <MethodInfo, Expression[], Expression>?invoker, MethodInfo method, ConstantExpression?service, bool simpleExceptionHandling) { _simpleExceptionHandling = simpleExceptionHandling; _invoker = invoker; _service = service; Method = method; }
public static Expression?ConstantToLite(Expression expression) { ConstantExpression?c = expression as ConstantExpression; if (c == null) { return(null); } if (c.Value == null) { return(c); } if (c.Type.IsLite()) { Lite <IEntity> lite = (Lite <IEntity>)c.Value; var id = GetPrimaryKeyValue(lite.IdOrNull, lite.EntityType); EntityExpression ere = new EntityExpression(lite.EntityType, new PrimaryKeyExpression(id), null, null, null, null, null, false); return(new LiteReferenceExpression(Lite.Generate(lite.EntityType), ere, null, false, false)); } return(null); }
/// <summary> /// 字段名 /// </summary> /// <param name="node"></param> /// <returns></returns> protected override Expression VisitMember(MemberExpression node) { switch (node.NodeType) { case ExpressionType.MemberAccess: this.AppendParameter(node, new Stack <MemberInfo>()); break; case ExpressionType.Constant: { ConstantExpression?constant = (ConstantExpression?)node.Expression; if (constant != null) { switch (node.Member.MemberType) { case MemberTypes.Property: this.AppendParameter(((PropertyInfo)node.Member).GetValue(constant.Value)); break; case MemberTypes.Field: this.AppendParameter(((FieldInfo)node.Member).GetValue(constant.Value)); break; } } } break; default: break; } return(node); }
public static ConstantExpressionSurrogate?Convert(ConstantExpression?source) => source is null ? null : new ConstantExpressionSurrogate { Type = source.Type, Value = Value.Wrap(source.Value), };
private Expression ReduceConstantTypeEqual() { ConstantExpression?ce = Expression as ConstantExpression; //TypeEqual(null, T) always returns false. if (ce !.Value == null) { return(Utils.Constant(value: false)); }
protected override Expression VisitConstant(ConstantExpression node) { if (OeExpressionHelper.GetCollectionItemTypeOrNull(node.Type) == _sourceType) { _source = node; } return(node); }
/// <summary>Determines whether the specified <see cref="ConstantExpression"/>s are equal.</summary> /// <param name="x">The first <see cref="ConstantExpression"/> to compare.</param> /// <param name="y">The second <see cref="ConstantExpression"/> to compare.</param> /// <returns>true if the specified <see cref="ConstantExpression"/>s are equal; otherwise, false.</returns> bool IEqualityComparer <ConstantExpression> .Equals([AllowNull] ConstantExpression?x, [AllowNull] ConstantExpression?y) { if (ReferenceEquals(x, y)) { return(true); } if (x == null || y == null) { return(false); } return(EqualsConstant(x, y, BeginScope())); }
private bool AddMethod <TService, TRequest, TResponse>( string serviceName, string operationName, MethodInfo method, MethodType methodType, object state, Func <MethodInfo, Expression[], Expression>?invoker, MarshallerCache marshallerCache, ConstantExpression?service) where TService : class where TRequest : class where TResponse : class { var grpcMethod = new Method <TRequest, TResponse>(methodType, serviceName, operationName, marshallerCache.GetMarshaller <TRequest>(), marshallerCache.GetMarshaller <TResponse>()); var stub = new MethodStub <TService>(invoker, method, service); return(TryBind <TService, TRequest, TResponse>(state, grpcMethod, stub)); }
// Fold all root references to exactly the same value protected override Expression VisitConstant(ConstantExpression node) { if (typeof(IGitQueryRoot).IsAssignableFrom(node.Type)) { if (_defaultRoot != null) { return(_defaultRoot); } if (node.Value != null) { return(_defaultRoot = node); } } return(base.VisitConstant(node)); }
/// <summary> /// Creates a new ConsistentWithExpressionNode. /// </summary> /// <param name="parseInfo">Method parse info.</param> /// <param name="mutationState">Mutation state for the query.</param> /// <param name="scanWait">Time to wait for index scan.</param> public ConsistentWithExpressionNode(MethodCallExpressionParseInfo parseInfo, ConstantExpression mutationState, ConstantExpression?scanWait) : base(parseInfo) { if (mutationState == null) { throw new ArgumentNullException(nameof(mutationState)); } if (mutationState.Type != typeof(MutationState)) { throw new ArgumentException($"{nameof(mutationState)} must return a {typeof(MutationState)}", nameof(mutationState)); } if (scanWait != null && scanWait.Type != typeof(TimeSpan)) { throw new ArgumentException($"{nameof(scanWait)} must return a {typeof(TimeSpan)}", nameof(scanWait)); } MutationState = mutationState; ScanWait = scanWait; }
/// <summary> /// Creates a new ScanConsistencyExpressionNode. /// </summary> /// <param name="parseInfo">Method parse info.</param> /// <param name="scanConsistency">Scan consistency for the query.</param> /// <param name="scanWait">Time to wait for index scan.</param> public ScanConsistencyExpressionNode(MethodCallExpressionParseInfo parseInfo, ConstantExpression scanConsistency, ConstantExpression?scanWait) : base(parseInfo) { if (scanConsistency == null) { throw new ArgumentNullException(nameof(scanConsistency)); } if (scanConsistency.Type != typeof(QueryScanConsistency)) { throw new ArgumentException($"{nameof(scanConsistency)} must return a {typeof(QueryScanConsistency)}", nameof(scanConsistency)); } if (scanWait != null && scanWait.Type != typeof(TimeSpan)) { throw new ArgumentException($"{nameof(scanWait)} must return a {typeof(TimeSpan)}", nameof(scanWait)); } ScanConsistency = scanConsistency; ScanWait = scanWait; }
/// <summary> /// converts constant expression to constant value /// </summary> /// <param name="expression">constant expression</param> /// <returns>constant value</returns> internal static string GetValueFromExpression(Expression expression) { if (expression.NodeType == ExpressionType.Constant) { string?constantValue = ((ConstantExpression)expression)?.Value?.ToString(); if (constantValue == null) { throw new InvalidQueryException($"null Constant value for expression : {expression?.ToString()}"); } return(constantValue); } else if (expression.NodeType == ExpressionType.Convert || expression.NodeType == ExpressionType.ConvertChecked) { ConstantExpression?constExpr = (expression as UnaryExpression)?.Operand as ConstantExpression; object? constExpValue = constExpr?.Value; int constVal = constExpValue == null ? 0 : (int)constExpValue; return(constVal.ToString()); } else if (expression.NodeType == ExpressionType.MemberAccess) { MemberExpression memberExpression = (MemberExpression)expression; FieldInfo? fieldInfo = memberExpression?.Member as FieldInfo; ConstantExpression?constantExpression = memberExpression?.Expression as ConstantExpression; string? constantValue = fieldInfo?.GetValue(constantExpression?.Value)?.ToString(); if (constantValue == null) { throw new InvalidQueryException($"null MemberAccess value for expression: {expression?.ToString()}"); } return(constantValue); } else { throw new InvalidQueryException( string.Format("The expression type {0} is not supported to obtain a value.", expression.NodeType)); } }
private static Expression BuildFactoryExpression( ConstructorInfo constructor, int?[] parameterMap, Expression serviceProvider, Expression factoryArgumentArray) { ParameterInfo[]? constructorParameters = constructor.GetParameters(); var constructorArguments = new Expression[constructorParameters.Length]; for (int i = 0; i < constructorParameters.Length; i++) { ParameterInfo?constructorParameter = constructorParameters[i]; Type? parameterType = constructorParameter.ParameterType; bool hasDefaultValue = ParameterDefaultValue.TryGetDefaultValue(constructorParameter, out object?defaultValue); if (parameterMap[i] != null) { constructorArguments[i] = Expression.ArrayAccess(factoryArgumentArray, Expression.Constant(parameterMap[i])); } else { var parameterTypeExpression = new Expression[] { serviceProvider, Expression.Constant(parameterType, typeof(Type)), Expression.Constant(constructor.DeclaringType, typeof(Type)), Expression.Constant(hasDefaultValue) }; constructorArguments[i] = Expression.Call(GetServiceInfo, parameterTypeExpression); } // Support optional constructor arguments by passing in the default value // when the argument would otherwise be null. if (hasDefaultValue) { ConstantExpression?defaultValueExpression = Expression.Constant(defaultValue); constructorArguments[i] = Expression.Coalesce(constructorArguments[i], defaultValueExpression); } constructorArguments[i] = Expression.Convert(constructorArguments[i], parameterType); } return(Expression.New(constructor, constructorArguments)); }
private bool AddMethod <TService, TRequest, TResponse>( string serviceName, string operationName, MethodInfo method, MethodType methodType, ServiceBindContext bindContext, Func <MethodInfo, Expression[], Expression>?invoker, MarshallerCache marshallerCache, ConstantExpression?service) where TService : class where TRequest : class where TResponse : class { var grpcMethod = new Method <TRequest, TResponse>(methodType, serviceName, operationName, marshallerCache.GetMarshaller <TRequest>(), marshallerCache.GetMarshaller <TResponse>()); var stub = new MethodStub <TService>(invoker, method, service); try { return(TryBind <TService, TRequest, TResponse>(bindContext, grpcMethod, stub)); } catch (Exception ex) { OnError(ex.Message); return(false); } }
/// <summary> /// Converts String.Compare expression to StringComparisonExpression /// </summary> /// <param name="leftString">String expression on the left side of the comparison</param> /// <param name="rightString">String expression on the right side of the comparison</param> /// <param name="operation">Comparison operation being performed</param> /// <param name="number">Number that String.Compare was being compared to, typically 0, 1, or -1.</param> /// <param name="comparisonExpression">Indicated the comparison type. Most commonly used with case insensitive comparisons</param> private Expression ConvertStringCompareExpression(Expression leftString, Expression rightString, ExpressionType operation, int number, ConstantExpression?comparisonExpression) { if (number == 1) { if ((operation == ExpressionType.LessThan) || (operation == ExpressionType.NotEqual)) { operation = ExpressionType.LessThanOrEqual; } else if ((operation == ExpressionType.Equal || operation == ExpressionType.GreaterThanOrEqual)) { operation = ExpressionType.GreaterThan; } else { // Always evaluates to true or false regardless of input, so return a constant expression return(Expression.Constant(operation == ExpressionType.LessThanOrEqual, typeof(bool))); } } else if (number == -1) { if ((operation == ExpressionType.GreaterThan) || (operation == ExpressionType.NotEqual)) { operation = ExpressionType.GreaterThanOrEqual; } else if ((operation == ExpressionType.Equal || operation == ExpressionType.LessThanOrEqual)) { operation = ExpressionType.LessThan; } else { // Always evaluates to true or false regardless of input, so return a constant expression return(Expression.Constant(operation == ExpressionType.GreaterThanOrEqual, typeof(bool))); } } else if (number > 1) { // Always evaluates to true or false regardless of input, so return a constant expression return(Expression.Constant( (operation == ExpressionType.NotEqual) || (operation == ExpressionType.LessThan) || (operation == ExpressionType.LessThanOrEqual), typeof(bool))); } else if (number < -1) { // Always evaluates to true or false regardless of input, so return a constant expression return(Expression.Constant( (operation == ExpressionType.NotEqual) || (operation == ExpressionType.GreaterThan) || (operation == ExpressionType.GreaterThanOrEqual), typeof(bool))); } // If number == 0 we just leave operation unchanged StringComparison?comparison = null; if (comparisonExpression != null) { comparison = (StringComparison)comparisonExpression.Value; if (comparison != StringComparison.Ordinal && comparison != StringComparison.OrdinalIgnoreCase) { var msg = $"String comparison option {comparison} is not supported"; throw new NotSupportedException(msg); } } return(StringComparisonExpression.Create(operation, leftString, rightString, comparison)); }
/// <summary> /// Converts <see cref="string.Compare(string, string)"/> expressions to a /// <see cref="Expressions.StringComparisonExpression"/>, if applicable /// </summary> /// <param name="expression">BinaryExpression to test and convert</param> /// <returns>If not a String.Compare expression, returns the original expression. Otherwise the converted expression.</returns> /// <remarks> /// Converts String.Compare and String.CompareTo clauses where compared to an integer. /// i.e. String.Compare(x, y) < 0 or x.CompareTo(y) < 0 are both the equivalent of x < y /// </remarks> public Expression Transform(BinaryExpression expression) { // See if one side is a call to String.Compare var leftExpression = expression.Left as MethodCallExpression; var rightExpression = expression.Right as MethodCallExpression; ConstantExpression?comparisonExpression = null; if ((leftExpression != null) && !StringCompareMethods.Contains(leftExpression.Method)) { leftExpression = null; } if ((rightExpression != null) && !StringCompareMethods.Contains(rightExpression.Method)) { rightExpression = null; } var methodCallExpression = leftExpression ?? rightExpression; if (methodCallExpression == null) { // Not a string comparison return(expression); } // Get the number side of the comparison, which must be a constant integer var numericExpression = leftExpression != null ? expression.Right as ConstantExpression : expression.Left as ConstantExpression; if ((numericExpression == null) || !typeof(int).IsAssignableFrom(numericExpression.Type)) { // Only convert if comparing to an integer return(expression); } var number = (int)numericExpression.Value; // Get the strings from the method call parameters Expression leftString; Expression rightString; if (methodCallExpression.Arguments.Count > 2) { leftString = methodCallExpression.Arguments[0]; rightString = methodCallExpression.Arguments[1]; comparisonExpression = methodCallExpression.Arguments[2] as ConstantExpression; } else if (methodCallExpression.Arguments.Count > 1) { leftString = methodCallExpression.Arguments[0]; rightString = methodCallExpression.Arguments[1]; } else { leftString = methodCallExpression.Object !; rightString = methodCallExpression.Arguments[0]; } if (leftExpression == null) { // If the method call is on the right side of the binary expression, then reverse the strings var temp = leftString; leftString = rightString; rightString = temp; } return(ConvertStringCompareExpression(leftString, rightString, expression.NodeType, number, comparisonExpression)); }
public static bool IsBase(this IQueryable query) { ConstantExpression?ce = query.Expression as ConstantExpression; return(ce != null && ce.Value == query); }
internal MethodStub(Func <MethodInfo, Expression[], Expression>?invoker, MethodInfo method, ConstantExpression?service) { _invoker = invoker; _service = service; Method = method; }