System.Linq.Expressions.Expression.Convert C# (CSharp) Method

Convert() public static method

Creates a UnaryExpression that represents a conversion operation for which the implementing method is specified.
/// or is null. /// is not null and the method it represents returns void, is not static (Shared in Visual Basic), or does not take exactly one argument. More than one method that matches the description was found. No conversion operator is defined between .Type and .-or-.Type is not assignable to the argument type of the method represented by .-or-The return type of the method represented by is not assignable to .-or-.Type or is a nullable value type and the corresponding non-nullable value type does not equal the argument type or the return type, respectively, of the method represented by .
public static Convert ( Expression expression, Type type, MethodInfo method ) : UnaryExpression
expression Expression An to set the property equal to.
type Type A to set the property equal to.
method System.Reflection.MethodInfo A to set the property equal to.
return UnaryExpression
        public static UnaryExpression Convert(Expression expression, Type type, MethodInfo method)
        {
            RequiresCanRead(expression, nameof(expression));
            ContractUtils.RequiresNotNull(type, nameof(type));
            TypeUtils.ValidateType(type, nameof(type));
            if (type.IsByRef)
            {
                throw Error.TypeMustNotBeByRef(nameof(type));
            }

            if (type.IsPointer)
            {
                throw Error.TypeMustNotBePointer(nameof(type));
            }

            if (method == null)
            {
                if (expression.Type.HasIdentityPrimitiveOrNullableConversionTo(type) ||
                    expression.Type.HasReferenceConversionTo(type))
                {
                    return new UnaryExpression(ExpressionType.Convert, expression, type, null);
                }
                return GetUserDefinedCoercionOrThrow(ExpressionType.Convert, expression, type);
            }
            return GetMethodBasedCoercionOperator(ExpressionType.Convert, expression, type, method);
        }

Same methods

Expression::Convert ( Expression expression, Type type ) : UnaryExpression

Usage Example

Ejemplo n.º 1
1
		Expression ReplaceParameter(IDictionary<Expression,Expression> expressionAccessors, Expression expression, Action<string> setName)
		{
			return expression.Convert(expr =>
			{
				if (expr.NodeType == ExpressionType.Constant)
				{
					var c = (ConstantExpression)expr;

					if (!ExpressionHelper.IsConstant(expr.Type) || _asParameters.Contains(c))
					{
						var val = expressionAccessors[expr];

						expr = Expression.Convert(val, expr.Type);

						if (expression.NodeType == ExpressionType.MemberAccess)
						{
							var ma = (MemberExpression)expression;
							setName(ma.Member.Name);
						}
					}
				}

				return expr;
			});
		}
All Usage Examples Of System.Linq.Expressions.Expression::Convert
Expression