The code below generates exception:
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBind开发者_高级运维erException: Cannot implicitly convert type 'void' to 'object'
var m = M((dynamic)d); //Exception thrown here
private void M(Int32 n) { Console.WriteLine("M(Int32): " + n); }
I think that null should be assigned to the m variable instead of exception.
Any idea?
Edit
Please note that below generates compile time error
dynamic result = M(1);//compile time error: Cannot implicitly convert type 'void' to 'dynamic'
private void M(Int32 n) { }
Normally questions like this are answered with a specification reference showing that the compiler / run-time is actually doing the right thing. In this case, the relevant section of the spec (7.2.2) is relatively silent. It basically says that the exact details are implementation-specific.
However, I would argue that it's doing the right thing: if you think of dynamic typing as roughly translating to letting the compiler operate in the same way, but using the actual types of expressions as they are at execution time, then it's entirely reasonable to have an execution time error. The normal compiler behaviour when calling a void method is to prohibit using it as the right-hand side of an assignment expression, so why should that change just because the binding is done at execution time?
精彩评论