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

TypeAs() public static method

Creates a UnaryExpression that represents an explicit reference or boxing conversion where null is supplied if the conversion fails.
/// or is null.
public static TypeAs ( Expression expression, Type type ) : UnaryExpression
expression Expression An to set the property equal to.
type Type A to set the property equal to.
return UnaryExpression
        public static UnaryExpression TypeAs(Expression expression, Type type)
        {
            RequiresCanRead(expression, nameof(expression));
            ContractUtils.RequiresNotNull(type, nameof(type));
            TypeUtils.ValidateType(type, nameof(type));

            if (type.GetTypeInfo().IsValueType && !type.IsNullableType())
            {
                throw Error.IncorrectTypeForTypeAs(type, nameof(type));
            }
            return new UnaryExpression(ExpressionType.TypeAs, expression, type, null);
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        ///     Create a lambda to receive a property value
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        private Delegate CreateValueGetter(PropertyInfo property)
        {
            ParameterExpression parameter = Expression.Parameter(GetType(), "i");
            UnaryExpression     cast      = Expression.TypeAs(Expression.Property(parameter, property), typeof(object));

            return(Expression.Lambda(cast, parameter).Compile());
        }
All Usage Examples Of System.Linq.Expressions.Expression::TypeAs
Expression