How to check if an expression is empty (void)?

What is the best way to check if an instance is System.Linq.Expressions.Expressionempty? For example, something like this:

Expression expression = Expression.Empty();
...
if (expression.IsEmpty) { ...

only that which IsEmptydoes not exist.

One idea is to check the result ToString:

if (expression.ToString() == "default(Void)") { ...

but that does not seem right.

+4
source share
1 answer

According to the documentation Empty()returns

Default selection, with the NodeType property equal by default and the Type property set to Void.

so you can use:

if(expression.NodeType == ExpressionType.Default && expression.Type == typeof(void))
+3
source

All Articles