Change return type in Interface Method of a Class (C#)
Posted on 2009-05-15
I'm in the process of building a set of custom classes to wrap up a custom OLEDB provider, so I'm recreating classes such as MyDbConnection, MyDbCommand etc. The problem is that when using an interface, such as IDbConnection, it returns an interface type.
For example, I have a MyDbCommand class (using IDbCommand) and a MyDbConnection class (using IDbConnection). When I provide an method for the MyDbConnection.CreateCommand() it returns a IDbCommand, not a MyDbCommand as I want it to.
When I look at the 'standard' classes such as OleDbConnection, it's CreateCommand() method returns an OleDbCommand object, not a IDbCommand object so I'm guessing this is possible?
The method in the MyDbConnection as the moment looks like:
public IDbCommand CreateCommand()
return new MyDbCommand();
which compiles, but then requires conversion when used in another project. Am I missing a keyword/method in order to do this?