InvalidCastException driving me nuts

J

JPerrin

I'm attempting to create a generic data access layer that can load any
assembly that has objects implementing the "IDb..." interfaces.
Everything is working perfectly when I use a SqlConnection, however when
I attempt to use a MySqlConnection and cast it to an IDbConnection I'm
getting an InvalidCastException, even though the MySqlConnection class
implements the IDbConnection interface. Here's a snippet from my code:

------

if( this._assembly == null )
{
this._assembly = Assembly.Load( this.AssemblyName );
}

Object obj = Activator.CreateInstance(
this._assembly.GetType( this._connectionType ) );

Console.WriteLine( obj.GetType().ToString() );
Type [] types = obj.GetType().GetInterfaces();
foreach( Type type in types )
{
Console.Write( type.Name + ", " );
}

IDbConnection conn = (IDbConnection)obj;
conn.ConnectionString = this._connectionString;

return conn;

------

The exception is thrown at the "IDbConnection conn =
(IDbConnection)obj;" line. The output from my Console.WriteLine calls
is as follows:

ByteFX.Data.MySqlClient.MySqlConnection
IComponent, IDisposable, IDbConnection, ICloneable,

As you can see, the Activator.CreateInstance method is returning an
object of the correct type, and the little foreach loop proves that the
object *does* indeed implement IDbConnection. When I run the same code
with a SqlConnection object it works perfectly.

If anybody has any ideas as to what may be happening here, I'd love to
hear them!

- -
JP
 
J

Jon Skeet [C# MVP]

JPerrin said:
I'm attempting to create a generic data access layer that can load any
assembly that has objects implementing the "IDb..." interfaces.
Everything is working perfectly when I use a SqlConnection, however when
I attempt to use a MySqlConnection and cast it to an IDbConnection I'm
getting an InvalidCastException, even though the MySqlConnection class
implements the IDbConnection interface. Here's a snippet from my code:

I suggest you find out exactly *which* IDbConnection it implements -
and where that type was loaded from. For example, if the assembly
containing MySqlConnection contains the definition for IDbConnection as
well, you'll have problems.

See http://www.pobox.com/~skeet/csharp/plugin.html for more information
about how this kind of thing can go wrong.
 
J

JPerrin

Jon said:
I suggest you find out exactly *which* IDbConnection it implements -
and where that type was loaded from. For example, if the assembly
containing MySqlConnection contains the definition for IDbConnection as
well, you'll have problems.

See http://www.pobox.com/~skeet/csharp/plugin.html for more information
about how this kind of thing can go wrong.

Thanks Skeeter, that article really helped to point me in the right
direction. Here's what was happening:

- I was dynamically loading a ByteFX.MySqlClient assembly that was
pre-compiled with the .NET 1.1 framework.

- My project was compiled with VS.NET 2002, which uses the 1.0 version
of the framework.

When I attempted to cast the loaded MySqlConnection object as an
IDbConnection object, the runtime was unable to perform the cast because
of the difference in .NET framework versions. As soon as I created the
ByteFX assembly with VS.NET 2002, and thus compiled it against 1.0,
everything worked just fine.

I am now once again a happy camper.

- -
JP
 
Top