When an operation invokes an Expression.Call, it will throw an error stating that ‘System.Boolean’ cannot be converted to ‘System.Void’
More specifically:
I am implementing a ‘wait’ keyword which will simply call WaitOne() on a specified EventWaitHandle, and I am not interested in the return type, as it is supposed to wait indefinitely.
I have tried Expression.Convert(expression, typeof(void)), but it didn’t really do anything interesting.
edit: I found one solution: put the expression in a block. Not sure why it has any effect.
From the description it sounds like you were trying to create an
Expression<Action>.However, the
WaitOnemethod has the signaturebool WaitOne()thus the resulting expression was trying to return abool.By wrapping the expression in a block you created a small thunk method that has no return value (
void).E.g. given the following code:
The compiler cannot directly create a delegate of type
ActionforWaitOnebecause that would unbalance the stack; ie. the return value fromWaitOneneeds to be removed from the stack.So to help you the compiler silently creates a method, thus what is actually compiled is similar to:
When building the expression yourself you don’t have the luxury of the compiler doing this for you. So in essence, when you created the block, you manually did this same step that the compiler would normally perform.