/// <summary>
        /// Creates a <see cref="ConstantExpression"/> that has the <see cref="P:ConstantExpression.Value"/>
        /// and <see cref="P:ConstantExpression.Type"/> properties set to the specified values. .
        /// </summary>
        /// <param name="value">An <see cref="System.Object"/> to set the <see cref="P:ConstantExpression.Value"/> property equal to.</param>
        /// <param name="type">A <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param>
        /// <returns>
        /// A <see cref="ConstantExpression"/> that has the <see cref="P:Expression.NodeType"/> property equal to
        /// <see cref="F:ExpressionType.Constant"/> and the <see cref="P:ConstantExpression.Value"/> and
        /// <see cref="P:Expression.Type"/> properties set to the specified values.
        /// </returns>
        public static ConstantExpression Constant(object value, Type type)
        {
            ContractUtils.RequiresNotNull(type, nameof(type));
            if (value == null ? type.GetTypeInfo().IsValueType&& !TypeUtils.IsNullableType(type) : !type.IsAssignableFrom(value.GetType()))
            {
                throw Error.ArgumentTypesMustMatch();
            }

            return(ConstantExpression.Make(value, type));
        }
Example #2
0
        //CONFORMING
        public static ConstantExpression Constant(object value)
        {
            if (value == null)
            {
                return(ConstantExpression.NullLiteral);
            }

            Type t = value.GetType();

            if (!t.IsEnum)
            {
                switch (Type.GetTypeCode(t))
                {
                case TypeCode.Boolean:
                    return(Constant((bool)value));

                case TypeCode.Int32:
                    int x          = (int)value;
                    int cacheIndex = x + 2;
                    if (cacheIndex >= 0 && cacheIndex < ConstantExpression.IntCache.Length)
                    {
                        ConstantExpression res;
                        if ((res = ConstantExpression.IntCache[cacheIndex]) == null)
                        {
                            ConstantExpression.IntCache[cacheIndex] = res = ConstantExpression.Make(x, typeof(int));
                        }
                        return(res);
                    }
                    break;

                case TypeCode.String:
                    if (String.IsNullOrEmpty((string)value))
                    {
                        return(ConstantExpression.EmptyStringLiteral);
                    }
                    break;
                }
            }

            return(ConstantExpression.Make(value, value == null ? typeof(object) : value.GetType()));
        }
Example #3
0
 /// <summary>
 /// Creates a <see cref="ConstantExpression"/> that has the <see cref="P:ConstantExpression.Value"/> property set to the specified value. .
 /// </summary>
 /// <param name="value">An <see cref="System.Object"/> to set the <see cref="P:ConstantExpression.Value"/> property equal to.</param>
 /// <returns>
 /// A <see cref="ConstantExpression"/> that has the <see cref="P:Expression.NodeType"/> property equal to
 /// <see cref="F:ExpressionType.Constant"/> and the <see cref="P:Expression.Value"/> property set to the specified value.
 /// </returns>
 public static ConstantExpression Constant(object value)
 {
     return(ConstantExpression.Make(value, value == null ? typeof(object) : value.GetType()));
 }