派生烦人的 ASP.NET 类
我想从 System.Web.UI.WebControls.WebControl 派生一个新类,但是我认为当您派生一个类时,您不会继承它的构造函数。
这造成的困难在于,该类的构造函数之一有一个类型为System.Web.UI.HtmlTextWriterTag
的参数,它定义了创建的控件/元素的html标签,而没有办法在类构建/初始化后设置它。
有人对如何解决这个问题有任何建议吗?
I would like to derive a new class from System.Web.UI.WebControls.WebControl
, however I think when you derive a class you dont inherit it's constructors.
The difficulty this causes is that one of the constructors for this class has a parameter of type System.Web.UI.HtmlTextWriterTag
, which defines the html tag of the control/element created, and there is no way to set this after the class has been constructed/initialized.
Has anyone got any suggestions on how to work through this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该有帮助:
ASP.NET 控件开发速成课程:从现有类派生新控件
只要遵循这个速成课程,您就会发现它是多么简单。
This should help:
A Crash Course on ASP.NET Control Development: Deriving New Controls from Existing Classes
just follow this crash course and you will see how simple it is.
像这样?
Like this?
您需要将相同的构造函数添加到您自己的类型中。然后使用 :base(...) 调用基本构造函数。
您还可以在类型和基类型之间使用不匹配的签名,只要
:base(...)
的签名与基类型构造函数的签名匹配即可。但您只能使用表达式来定义传递给它的参数的值。所以您可能想使用一些静态辅助方法。You need should add the same constructors to your own type. And then use :base(...) to call the base constructors.
You can also use non matching signatures between your type and the base type, as long as the signature of
:base(...)
matches the signature of the base type constructor. But you can only use expressions to define the value of the parameters passed to it. So you might want to use some static helper methods.当你派生一个基类时,基类的构造函数也会被派生。您可以将
HtmlTextWriter
类型传递给派生类 ctr,并在派生类中将 ctr 定义为DervCtr(HtmlTextWriter) : base(HtmlTextWriter)
When you derive a base class, the base class constructor will also be derived. You can pass the
HtmlTextWriter
type to the derived class ctr and in the derived class define the ctr asDervCtr(HtmlTextWriter) : base(HtmlTextWriter)