Suppose that you have 2 inherited classes:
As shown above in the parent class Boo there is explicit cast operator from string (the fact that it returns null is not relevant for us). With this operator you may cast Bar from string:
Also you may cast to Foo and compiler will allow to do that:
Now let’s try to construct expression tree which converts string to Foo:
It will compile but will throw exception “No coercion operator is defined between types 'System.String' and 'Foo'”. This looks strange because compiler allows to do the same cast directly.
Unfortunately no luck here as well: it throws exception “The operands for operator 'Convert' do not match the parameters of method 'op_Explicit'”.
If you will try to get op_Explicit using Foo type:
you will again get “No coercion operator is defined between types 'System.String' and 'Foo'”” error. The same behavior exist both in .Net 3.5 and 4.0. This problem looks like a bug in expression trees.