简体   繁体   English

覆盖抽象类的属性?

[英]Overriding a property of an abstract class?

Background: I am using the System.Data.Common.DBConnect class for a set of classes that connect to different types of data sources like CSV, AD, SharePoint, SQL, Excel, SQL etc. There is an interface that defines the contract for all the Datasource types. 背景:我使用System.Data.Common.DBConnect类来连接一组类,这些类连接到不同类型的数据源,如CSV,AD,SharePoint,SQL,Excel,SQL等。有一个界面定义合同所有数据源类型。

I wished to use the connectionString property of the DBConnection object to store file paths on the file-based sources to pass to the GetData(DBConnection conn) methods for the file-based data sources. 我希望使用DBConnection对象的connectionString属性在基于文件的源上存储文件路径,以传递给基于文件的数据源的GetData(DBConnection conn)方法。 This does not work, as there is some validation happening when assigning a string the the ConnectionStribg proprty. 这不起作用,因为在为ConnectionStribg proprty分配字符串时会发生一些验证。 My question: How do I create my own class derived from the DBConnection Class (it is an abstract class) that ONLY ADDS one property called ParameterString? 我的问题:如何创建自己的DBConnection类(它是一个抽象类)派生的类,它只能添加一个名为ParameterString的属性?

tldr; tldr; I want to inherit from System.Data.Common.DBConnect and add my own string property. 我想继承System.Data.Common.DBConnect并添加我自己的字符串属性。 How? 怎么样?

EDIT 编辑

The interface is as follows: 界面如下:

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

}

You can inherit from DBConnection, but the problem is you would need to implemented all of the inherited abstract members (of which there are 22): 您可以从DBConnection继承,但问题是您需要实现所有继承的抽象成员(其中有22个):

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

   //Still need to implement all of the 
}

I'm assuming that you actually want to take advantage of the built in ADO classes that handle the implementation of DBConnection , so this is not a good option. 我假设你真的想利用处理DBConnection实现的内置ADO类,所以这不是一个好的选择。

Perhaps you just need to keep track of the information separately. 也许您只需要分别跟踪信息。 Is there a particular reason why the information has to be part of the connection class? 信息必须是连接类的一部分有特殊原因吗?

You could do something along the lines of: 你可以做一些事情:

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

   public string FileNames { get; set; }  
}

This would put the information together but not complicate the usage of the DBConnection class. 这会将信息放在一起,但不会使DBConnection类的使用复杂化。

The DbConnection class is abstract, so you must implement all of its abstract methods. DbConnection类是抽象的,因此您必须实现其所有抽象方法。 Here is what that looks like: 这是看起来像:

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(); }
}

Granted, you will have to put the correct logic in each of the methods that are throwing exceptions below. 当然,您必须在下面抛出异常的每个方法中放入正确的逻辑。

The ConnectionString property gives you an example on how to override a property. ConnectionString属性为您提供有关如何覆盖属性的示例。 If you need an additional property you can add it as you would any other property to a C# class. 如果您需要其他属性,可以将其添加到C#类的任何其他属性中。

Make your class abstract too... 让你的课也抽象......

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

        get; set;
    }   
}

If you don't wish your class to be abstract, then either inherit it from a concrete class, or override abstract methods. 如果您不希望您的类是抽象的,那么要么从具体类继承它,要么覆盖抽象方法。 No third option here... 这里没有第三种选择......

Thanks to RQDQ: I finally used the following class to do what I needed: 感谢RQDQ:我最后使用以下课程来完成我需要的工作:

public class GenericConnection
{
    public GenericConnection(){}

    public DbConnection DBConn { get; set; }

    public string Filename { get; set; }

}

As you can see, I added the System.Data.Common.DBConnect as an attribute, and added the string attribute I needed too. 如您所见,我添加了System.Data.Common.DBConnect作为属性,并添加了我需要的字符串属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM