Type Casting at Runtime .Net

  • Thread starter Thread starter CMaster383
  • Start date Start date
C

CMaster383

Language: VB.NET (could apply to C#)

Hey,

I am working on a component that will take any of the ADO.Net
connection Classes, OleDb, ODBC, SQL, and Oracle, and return the
appropriate Command Object for that type of connection. I have this
working successfully, but I am running into one flaw. I would like to
use Option Strict on the project, (which I assume will make the code
run faster).

But, in order to achieve this ability to return a Command Object at
runtime, I am forced to return the Object as a System.Object. From
there, I can not run the ExecuteReader method, or any of the other
methods on the object, if the Option Strict is enabled. I have tried
using the GetType method with CType, DirectCast and
'System.ComponentModel.TypeConverter' ConvertTo method with no luck.

My question boils down to this: Is there a way to perform Type
Casting by referencing the objects Type (GetType) and then performing
a type-cast at runtime?

I know this may sounds/seem a bit odd, but I was hoping it would be
possible since all of the Command Objects under .Net are written with
the same Methods (Thanks to microsoft for that). Any information would
be appreciate, Thanks

-Kevin
 
CMaster383 said:
I am working on a component that will take any of the ADO.Net
connection Classes, OleDb, ODBC, SQL, and Oracle, and return the
appropriate Command Object for that type of connection. I have this
working successfully, but I am running into one flaw. I would like to
use Option Strict on the project, (which I assume will make the code
run faster).

But, in order to achieve this ability to return a Command Object at
runtime, I am forced to return the Object as a System.Object.

Nope - return it as IDbCommand instead, which all the command classes
implement, and then you can use the methods defined in IDbCommand.
 
OK, I didn't realize that they implemented an interface. I looked in
the SDK that came with VS.Net and ...for whatever reason... the
documentation didn't make any reference to any interfaces.

I went out to MSDN and looked, and it does make reference to the
interfaces that the ADO.Net classes implement. That should fix my
problem. I assumed I would have to use interfaces instead of
attempting to type cast. Guess this proves my thought. Thanks!

-Kevin
 
Back
Top