派生烦人的 ASP.NET 类

发布于 2024-10-20 22:55:18 字数 227 浏览 0 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┊风居住的梦幻卍 2024-10-27 22:55:18

这应该有帮助:
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.

无尽的现实 2024-10-27 22:55:18

像这样?

public class MyWebControl: WebControl
{
    public MyWebControl(HtmlTextWriterTag tag) : base(tag) 
    {

    }               
}

Like this?

public class MyWebControl: WebControl
{
    public MyWebControl(HtmlTextWriterTag tag) : base(tag) 
    {

    }               
}
月棠 2024-10-27 22:55:18

您需要将相同的构造函数添加到您自己的类型中。然后使用 :base(...) 调用基本构造函数。

public MyType(ParamClass a)
    :base(a)
{
}

您还可以在类型和基类型之间使用不匹配的签名,只要 :base(...) 的签名与基类型构造函数的签名匹配即可。但您只能使用表达式来定义传递给它的参数的值。所以您可能想使用一些静态辅助方法。

You need should add the same constructors to your own type. And then use :base(...) to call the base constructors.

public MyType(ParamClass a)
    :base(a)
{
}

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.

莫相离 2024-10-27 22:55:18

当你派生一个基类时,基类的构造函数也会被派生。您可以将 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 as DervCtr(HtmlTextWriter) : base(HtmlTextWriter)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文