When compiling code at runtime using expression trees, we may find ourselves needing to check objects of indeterminate types for equality.
If we were to simply code this up by hand for each case, the compiler would take into account many things for us. The most ones that come to mind are:
- If a generic overload
T1.Equals<T2>
orT2.Equals<T1>
is available, that is used. - Otherwise, if either type has an implicit operator that would let us apply (1), that is used.
- Otherwise,
bool Equals(object)
is used (taking into account potential overrides, of course).
Regrettably, Expression.Equal(Expression left, Expression right)
does not do all these things for us.
How can we achieve the same behavior that the compiler normally provides?