Why is an ExpandoObject breaking code that otherwise works just fine?
Looking at the exception being thrown, it seems that even though OpenConnection returns a static type (DbConnection) and CreateCommand returns a static type (DbCommand), because the parameter passed to DbConnection is of type dynamic it's essentially treating the following code as a dynamic binding site:
var cmd = CreateCommand(ex);
cmd.Connection = conn;
Because of this, the runtime-binder is trying to find the most specific binding possible, which would be to cast the connection to SqlConnection. Even though the instance is technically a SqlConnection, it's statically typed as DbConnection, so that's what the binder attempts to cast from. Since there's no direct cast from DbConnection to SqlConnection, it fails.
What seems to work, taken from this S.O. answer dealing with the underlying exception type, is to actually declare conn as dynamic, rather than using var, in which case the binder finds the SqlConnection -> SqlConnection setter and just works, like so:
public static dynamic DynamicWeirdness()
{
dynamic ex = new ExpandoObject();
ex.TableName = "Products";
using (dynamic conn = OpenConnection())
{
var cmd = CreateCommand(ex);
cmd.Connection = conn;
}
Console.WriteLine("It worked!");
Console.Read();
return null;
}
That being said, given the fact that you statically typed the return type of CreateCommand to DbConnection, one would have thought the binder would have done a better job of "doing the right thing" in this case, and this may well be a bug in the dynamic binder implementation in C#.
When you pass the dynamic to CreateCommand
, the compiler is treating its return type as a dynamic that it has to resolve at runtime. Unfortunately, you're hitting some oddities between that resolver and the C# language. Fortunately, it's easy to work around by removing your use of var
forcing the compiler to do what you expect:
public static dynamic DynamicWeirdness() {
dynamic ex = new ExpandoObject ();
ex.Query = "SELECT * FROM Products";
using (var conn = OpenConnection()) {
DbCommand cmd = CreateCommand(ex); // <-- DON'T USE VAR
cmd.Connection = conn;
}
Console.WriteLine("It worked!");
Console.Read();
return null;
}
This has been tested on Mono 2.10.5, but I'm sure it works with MS too.
Because you're using dynamic as the argument to CreateCommand()
, the cmd
variable is also dynamic, which means its type is resolved at runtime to be SqlCommand
. By contrast, the conn
variable is not dynamic and is compiled to be of type DbConnection
.
Basically, SqlCommand.Connection
is of type SqlConnection
, so the conn
variable, which is of type DbConnection
, is an invalid value to set Connection
to. You can fix this by either casting conn
to an SqlConnection
, or making the conn
variable dynamic
.
The reason it worked fine before was because cmd
was actually a DbCommand
variable (even so it pointed to the same object), and the DbCommand.Connection
property is of type DbConnection
. i.e. the SqlCommand
class has a new
definition of the Connection
property.
Source issues annotated:
public static dynamic DynamicWeirdness() {
dynamic ex = new ExpandoObject();
ex.TableName = "Products";
using (var conn = OpenConnection()) { //'conn' is statically typed to 'DBConnection'
var cmd = CreateCommand(ex); //because 'ex' is dynamic 'cmd' is dynamic
cmd.Connection = conn;
/*
'cmd.Connection = conn' is bound at runtime and
the runtime signature of Connection takes a SqlConnection value.
You can't assign a statically defined DBConnection to a SqlConnection
without cast.
*/
}
Console.WriteLine("It will never get here!");
Console.Read();
return null;
}
Options for fixing source (pick only 1):
Cast to statically declare
conn
as a SqlConnection:using (var conn = (SqlConnection) OpenConnection())
Use runtime type of
conn
:using (dynamic conn = OpenConnection())
Don't dynamic bind CreateCommand:
var cmd = CreateCommand((object)ex);
Statically define
cmd
:DBCommand cmd = CreateCommand(ex);
It's acting as if you're trying to pass dynamics anonymous types across assemblies, which is not supported. Passing an ExpandoObject
is supported though. The work-around I have used, when I need to pass across assemblies, and I have tested it successfully, is to cast the dynamic input variable as an ExpandoObject
when you pass it in:
public static dynamic DynamicWeirdness()
{
dynamic ex = new ExpandoObject();
ex.TableName = "Products";
using (var conn = OpenConnection()) {
var cmd = CreateCommand((ExpandoObject)ex);
cmd.Connection = conn;
}
Console.WriteLine("It worked!");
Console.Read();
return null;
}
EDIT: As pointed out in the comments, you CAN pass dynamics across assemblies, you CAN'T pass anonymous types across assemblies without first casting them.
The above solution is valid for the same reason as Frank Krueger states above.
When you pass the dynamic to CreateCommand, the compiler is treating its return type as a dynamic that it has to resolve at runtime.