I am using the following code to convert some Json into a dynamic object. When I use DateTime.Parse on a property of my dynamic type I would expect the var to guess that it\
This is per the spec. See §7.6.5:
An invocation-expression is dynamically bound (§7.2.2) if at least one of the following holds:
• The primary-expression has compile-time type
dynamic
.• At least one argument of the optional argument-list has compile-time type
dynamic
and the primary-expression does not have a delegate type.
Consider this scenario:
class Foo {
public int M(string s) { return 0; }
public string M(int s) { return String.Empty; }
}
Foo foo = new Foo();
dynamic d = // something dynamic
var m = foo.M(d);
What should be the compile-time type of m
? The compiler can't tell, because it won't know until runtime which overload of Foo.M
is invoked. Thus, it says m
is dynamic.
Now, you could say that it should be able to figure out DateTime.Parse
has only one overload, and even if it didn't but all of its overloads have the same return type that in that case it should be able to figure out the compile-time type. That would be a fair point, and is probably best Eric Lippert. I asked a separate question to get insight into this: Why does a method invocation expression have type dynamic even when there is only one possible return type?.