覆盖抽象类的属性?

时间:2011-03-07 14:03:27

标签: c# inheritance override abstract-class

背景: 我将System.Data.Common.DBConnect类用于连接到不同类型的数据源(如CSV,AD,SharePoint,SQL,Excel,SQL等)的一组类。 有一个接口定义了所有Datasource类型的契约。

我希望使用DBConnection对象的connectionString属性在基于文件的源上存储文件路径,以传递给基于文件的数据源的GetData(DBConnection conn)方法。 这不起作用,因为在为ConnectionStribg proprty分配字符串时会发生一些验证。 我的问题: 如何创建自己的DBConnection类(它是一个抽象类)派生的类,只能添加一个名为ParameterString的属性?

tldr;我想继承System.Data.Common.DBConnect并添加我自己的字符串属性。怎么样?

修改

界面如下:

public interface IDataImport
{
    DbConnection CreateDbConnection(params string[] connectionString);

    DataSet GetResults(DbConnection conn, params string[] strQuery);

    DataTable GetAvailableTables(DbConnection conn);

   DataTable GetAvailableFields(DbConnection conn, string tableName);

}

4 个答案:

答案 0 :(得分:3)

您可以从DBConnection继承,但问题是您需要实现所有继承的抽象成员(其中有22个):

public class MyConnect : DBConnection
{
   public string FilePaths{ get; set; }

   //Still need to implement all of the 
}

我假设你真的想利用处理 DBConnection 实现的内置ADO类,所以这不是一个好的选择。

也许您只需要分别跟踪信息。是否有特殊原因导致信息必须成为连接类的一部分?

您可以采取以下措施:

public class MyConnectionInfo
{
   public DBConnection Connection { get; set; }

   public string FileNames { get; set; }  
}

这会将信息放在一起,但不会使DBConnection类的使用复杂化。

答案 1 :(得分:3)

DbConnection类是抽象的,因此您必须实现其所有抽象方法。这是看起来像:

protected override System.Data.Common.DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
{
    throw new System.NotImplementedException();
}

public override string ConnectionString
{
    get
    {
        throw new System.NotImplementedException();
    }
    set
    {
        throw new System.NotImplementedException();
    }
}

public override void ChangeDatabase(string databaseName)
{
    throw new System.NotImplementedException();
}

public override void Close()
{
    throw new System.NotImplementedException();
}

protected override System.Data.Common.DbCommand CreateDbCommand()
{
    throw new System.NotImplementedException();
}

public override string DataSource
{
    get { throw new System.NotImplementedException(); }
}

public override string Database
{
    get { throw new System.NotImplementedException(); }
}

public override void Open()
{
    throw new System.NotImplementedException();
}

public override string ServerVersion
{
    get { throw new System.NotImplementedException(); }
}

public override System.Data.ConnectionState State
{
    get { throw new System.NotImplementedException(); }
}

当然,你必须在下面抛出异常的每个方法中加入正确的逻辑。

ConnectionString属性为您提供了有关如何覆盖属性的示例。如果您需要一个额外的属性,您可以像将任何其他属性一样添加到C#类。

答案 2 :(得分:2)

让你的课也抽象......

public abstract class MyClass:System.Data.Common.DBConnect 
{
    abstract String ParameterString
    {

        get; set;
    }   
}

如果您不希望您的类是抽象的,那么要么从具体类继承它,要么覆盖抽象方法。这里没有第三种选择......

答案 3 :(得分:1)

感谢RQDQ:我最后使用以下课程来完成我需要的工作:

public class GenericConnection
{
    public GenericConnection(){}

    public DbConnection DBConn { get; set; }

    public string Filename { get; set; }

}

如您所见,我将System.Data.Common.DBConnect添加为属性,并添加了我需要的字符串属性。

相关问题