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

ReferenceEqual() public static method

Creates a BinaryExpression that represents a reference equality comparison.
public static ReferenceEqual ( Expression left, Expression right ) : BinaryExpression
left Expression An to set the property equal to.
right Expression An to set the property equal to.
return BinaryExpression
        public static BinaryExpression ReferenceEqual(Expression left, Expression right)
        {
            RequiresCanRead(left, nameof(left));
            RequiresCanRead(right, nameof(right));
            if (TypeUtils.HasReferenceEquality(left.Type, right.Type))
            {
                return new LogicalBinaryExpression(ExpressionType.Equal, left, right);
            }
            throw Error.ReferenceEqualityNotDefined(left.Type, right.Type);
        }

Usage Example

        // Helper that is used when re-eval of LHS is safe.
        private Expression ByValParameterTypeEqual(ParameterExpression value)
        {
            Expression getType = Expression.Call(value, typeof(object).GetMethod("GetType"));

            // In remoting scenarios, obj.GetType() can return an interface.
            // But JIT32's optimized "obj.GetType() == typeof(ISomething)" codegen,
            // causing it to always return false.
            // We workaround this optimization by generating different, less optimal IL
            // if TypeOperand is an interface.
            if (_typeOperand.GetTypeInfo().IsInterface)
            {
                var temp = Expression.Parameter(typeof(Type));
                getType = Expression.Block(new[] { temp }, Expression.Assign(temp, getType), temp);
            }

            // We use reference equality when comparing to null for correctness
            // (don't invoke a user defined operator), and reference equality
            // on types for performance (so the JIT can optimize the IL).
            return(Expression.AndAlso(
                       Expression.ReferenceNotEqual(value, Expression.Constant(null)),
                       Expression.ReferenceEqual(
                           getType,
                           Expression.Constant(_typeOperand.GetNonNullableType(), typeof(Type))
                           )
                       ));
        }
All Usage Examples Of System.Linq.Expressions.Expression::ReferenceEqual
Expression