覆盖一个方法(返回类型DBDataReader)并返回一个不同的类型(SQLDBDataReader)

本文关键字:一个 SQLDBDataReader 类型 返回类型 方法 覆盖 DBDataReader 返回 | 更新日期: 2023-09-27 18:19:17

我目前正在实现一个可以从不同数据库读取数据的小软件。代码如下:

interface Fetch
{
    Dictionary<string, DbDataReader> GetData();
}
abstract class Conn : Fetch
{ 
    abstract public void Connect();
    abstract public Dictionary<string, SqlDataReader> GetData();
}
class SqlConn : Conn 
{
    public override void Connect() 
    {
        _connection = new SqlConnection(_connectionString);
        try
        {
            _connection.Open();
        }
        catch (SqlException dbe)
        {
            throw dbe;
        }
    }
    public override Dictionary<string, SqlDataReader> GetData()
    {
        using (_connection)
        {
            Dictionary<string, SqlDataReader> dataDictionary = new Dictionary<string, SqlDataReader>();
            _xmlDoc.Load("Queries.xml");
            XPathNavigator navigator = _xmlDoc.CreateNavigator();
            XPathNodeIterator iterator = navigator.Select("//query");
            while (iterator.MoveNext())
            {
                _command = new SqlCommand(iterator.Current.ToString());
                _command.Connection = _connection;
                _command.CommandText = iterator.Current.ToString();
                SqlDataReader reader = _command.ExecuteReader() as SqlDataReader;
                dataDictionary.Add(iterator.Current.GetAttribute("name", ""), reader);
            }
            return dataDictionary;
        }
    }
}
class OraConn : Conn 
{
    public override void Connect()
    {
        _connection = new OracleConnection(_connectionString);
    }
    public override Dictionary<string, OracleDataReader> GetData()
    {
       using (_connection)
        {
            Dictionary<string, OracleDataReader> dataDictionary = new Dictionary<string, OracleDataReader>();
            _xmlDoc.Load("Queries.xml");
            XPathNavigator navigator = _xmlDoc.CreateNavigator();
            XPathNodeIterator iterator = navigator.Select("//query");
            while (iterator.MoveNext())
            {
                _command = new OracleCommand(iterator.Current.ToString());
                _command.Connection = _connection;
                _command.CommandText = iterator.Current.ToString();
                OracleDataReader reader = _command.ExecuteReader() as OracleDataReader;
                dataDictionary.Add(iterator.Current.GetAttribute("name", ""), reader);
            }
            return dataDictionary; 
        }
    }
}

但我的问题是返回类型,SQLDataReader和OraDataReader在派生类。这会导致编译器错误,提示

' Error 2 'DashBoard.Connection.OraConn.GetData()':返回类型必须为'System.Collections.Generic '。字典'以匹配重写的成员'"。

我该如何解决这个问题?还是有其他方法来实现这个函数?谢谢你!

覆盖一个方法(返回类型DBDataReader)并返回一个不同的类型(SQLDBDataReader)

方法签名必须保持不变,但只要你的子类继承DbDataReader,你可以返回其他DbDataReader类型,如SqlDataReader,它将编译。

例如:

abstract class DbDataReader
{
    // ...
}
class SqlDataReader : DbDataReader
{
}
// ...
class SqlConn : Conn 
{
    public override Dictionary<string, DbDataReader> GetData()
    {
       return new Dictionary<string, DbDataReader>
       {
            { "Key", new SqlDataReader() } 
       }
    }
}

您可以这样尝试:

public interface IValue<T> { T GetValue(); }
public class SomeClass : IValue<DbDataReader>, IValue<SqlDataReader>
{
    DbDataReader IValue<DbDataReader>.GetValue() { return objDbDataReader; }
    SqlDataReader IValue<SqlDataReader>.GetValue() { return objSqlDataReader; }
}
相关文章: