重写抽象类的属性?
背景: 我使用 System.Data.Common.DBConnect 类作为一组连接到不同类型数据源(如 CSV、AD、SharePoint、SQL、Excel、SQL 等)的类。 有一个接口定义了所有数据源类型的契约。
我希望使用 DBConnection 对象的 connectionString 属性来存储基于文件的源上的文件路径,以传递给基于文件的数据源的 GetData(DBConnection conn) 方法。 这不起作用,因为在为 ConnectionStrig 属性分配字符串时会发生一些验证。 我的问题: 如何创建我自己的从 DBConnection 类(它是一个抽象类)派生的类,该类仅添加一个名为 ParameterString 的属性?
太棒了;我想继承 System.Data.Common.DBConnect
并添加我自己的字符串属性。如何?
EDIT
界面如下:
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);
}
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.
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.
This does not work, as there is some validation happening when assigning a string the the 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?
tldr; I want to inherit from System.Data.Common.DBConnect
and add my own string property. 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以从 DBConnection 继承,但问题是您需要实现所有继承的抽象成员(其中有 22 个):
我假设您实际上想要利用处理实现的内置 ADO 类DBConnection,所以这不是一个好的选择。
也许您只需要单独跟踪这些信息。是否有特殊原因导致信息必须成为连接类的一部分?
您可以执行以下操作:
这会将信息放在一起,但不会使 DBConnection 类的使用变得复杂。
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):
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.
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:
This would put the information together but not complicate the usage of the DBConnection class.
DbConnection 类是抽象的,因此您必须实现它的所有抽象方法。看起来是这样的:
当然,您必须将正确的逻辑放入下面抛出异常的每个方法中。
ConnectionString 属性为您提供了有关如何覆盖属性的示例。如果您需要其他属性,您可以将其添加到 C# 类中,就像添加任何其他属性一样。
The DbConnection class is abstract, so you must implement all of its abstract methods. Here is what that looks like:
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. If you need an additional property you can add it as you would any other property to a C# class.
让你的类也抽象...
如果你不希望你的类是抽象的,那么要么从具体类继承它,要么重写抽象方法。这里没有第三个选择...
Make your class abstract too...
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...
感谢 RQDQ:我最终使用以下类来完成我需要的操作:
如您所见,我添加了 System.Data.Common.DBConnect 作为属性,并添加了我需要的字符串属性。
Thanks to RQDQ: I finally used the following class to do what I needed:
As you can see, I added the System.Data.Common.DBConnect as an attribute, and added the string attribute I needed too.