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

Constant() public static method

Creates a ConstantExpression that has the ConstantExpression.Value and ConstantExpression.Type properties set to the specified values. .
public static Constant ( object value, Type type ) : ConstantExpression
value object An to set the property equal to.
type Type A to set the property equal to.
return ConstantExpression
        public static ConstantExpression Constant(object value, Type type)
        {
            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 (value == null)
            {
                if (type == typeof(object))
                {
                    return new ConstantExpression(null);
                }

                if (!type.GetTypeInfo().IsValueType || type.IsNullableType())
                {
                    return new TypedConstantExpression(null, type);
                }
            }
            else
            {
                Type valueType = value.GetType();
                if (type == valueType)
                {
                    return new ConstantExpression(value);
                }

                if (type.IsAssignableFrom(valueType))
                {
                    return new TypedConstantExpression(value, type);
                }
            }

            throw Error.ArgumentTypesMustMatch();
        }
    }

Same methods

Expression::Constant ( object value ) : ConstantExpression

Usage Example

示例#1
0
 static Expression GetArgument(Expression Expression, int n)
 {
     if (Expression.Type == typeof(LuaArguments))
         return Expression.Property(Expression, "Item", Expression.Constant(n));
     else
         return Expression.ArrayAccess(Expression, Expression.Constant(n));
 }
All Usage Examples Of System.Linq.Expressions.Expression::Constant
Expression