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
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