为什么SqlDataReader不继承 [英] Why SqlDataReader is Not Inherit

查看:61
本文介绍了为什么SqlDataReader不继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET Framework中

SqlDataReader类哪个属性具有为什么我不能继承它的原因

还是我们可以创建一个没有密封的类的关键字?在我们的应用程序中不能像SqlDataReader类那样继承该类,而该类也是Public类



In The .NET Framework

SqlDataReader class which property have that why i can''t Inherit it

or can we make class with out seal Keyword That is not inherited in our application like SqlDataReader Class That is also Public class



// Summary:
    //     Provides a way of reading a forward-only stream of rows from a SQL Server
    //     database. This class cannot be inherited.
    public class SqlDataReader : DbDataReader, IDataReader, IDisposable, IDataRecord
     
     {
      //
      //
      //
      //
      .
      .   
      .
      .
      .

     }

推荐答案

请参阅msdn说明

http://msdn.microsoft.com/en-us/library/system. data.sqlclient.sqldatareader.aspx [ ^ ]
refer msdn explanation

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx[^]


您可以创建一个无法继承的类,如果它没有子类可以看到的构造函数(即没有受保护或公共的内容) ).您可以通过仅具有内部(或受保护的内部)构造函数来创建一个无法在其所驻留的程序集外部继承的函数.不过,我要说的是,如果您打算使一个类不可继承,则应将其标记为密封的(Microsoft应该在此处将其标记为密封的,除非它是内部可继承的(可能是这样).)
You can create a class that can''t be inherited if it has no constructors that a subclass can see (i.e. nothing protected or public). You can create one that can''t be inherited outside the assembly it resides in by having only internal (or protected internal) constructors. I would say, though, that if your intention is to make a class non-inheritable, you should mark it as sealed (and Microsoft should have done so here unless it is internally inheritable, which it probably is).


SqlDataReader类不是密封的,但是您不能从中继承,因为它不是可以实例化的类. IE.你不能说:
The SqlDataReader class is not sealed, but you cannot inherit from it as it is not a class you can instantiate. I.e. you cannot say:
SqlDataReader sdr = new SqlDataReader();


如果您考虑一下,这很有道理.数据读取器仅用于读取从SQL命令执行返回的所有信息,因此创建该数据读取器的新实例完全无用-它没有要读取的SQL结果.因此,获取实例的唯一方法是使用ExecuteReader方法从SQLCommand请求实例.

因此,如果您无法创建实例,为什么可以从该实例继承呢?您根本无法创建继承的实例-您不能将类升级"到派生版本,因为框架必须发明"所有缺少的信息.

您想实现什么,您认为这是个好主意?


If you think about it, it makes a lot of sense. A data reader is only used to read all the information returned from a SQL Command execution, so creating a new instance of it completely useless - it has no SQL results to read through. So, the only way you can get an instance is by requesting it from an SQLCommand by using the ExecuteReader method.

So, if you can''t create an instance, why would it be possible to inherit from it? You can''t create the inherited instance at all - you cannot "upgrade" a class to a derived version, as the framework would have to "invent" all the missing information.

What are you trying to achieve, that you think this would be a good idea?


这篇关于为什么SqlDataReader不继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆