ASP.NET:实现 Init 和 Dispose 方法

发布于 2024-10-31 21:24:04 字数 360 浏览 2 评论 0原文

ASP.NET Web 应用程序是否可以只有一个 Init 和一个 Dispose 方法,或者我可以为每个我想要关联这些方法的类实现这些方法吗?

更具体地说,我有 Customer 组件和 CustomerRecord 类,并且希望在这两个组件中实现 InitDispose 方法他们。

执行此操作的正确方法是什么?

要求:

我希望为每个上述类拥有独立的 InitDispose 方法。

Can an ASP.NET web application have only one Init and one Dispose method or can I implement these per class for those which I want to associate such methods?

More specifically I have Customer component and a CustomerRecord classes and would like to implement Init and Dispose methods in both of them.

What is the proper way to do this?

Requirement:

I want to have independent Init and Dispose methods for each aforementioned class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

翻身的咸鱼 2024-11-07 21:24:04

对于应该可处置的类,通过公开公共 Dispose 方法,必须实现 IDispsable 接口,以使“可处置性”在显式用户处置范围之外有效。这已经在很多地方被多次提及,包括这里,例如:

public class Customer : IDisposable
{
    public void Dispose()
    {
        Dispose(true);
        GC.SupressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            //dispose of managed resources
        }
        //dispose of unmanaged resources
    }

    ~Customer()
    {
        Dispose(false);
    }
}

请注意,析构函数(以波形符~开头的方法)可能不是必需的,但请阅读我上面链接的答案中的详细信息,以清楚地了解什么的情况和为什么 - 这只是回答你的直接提问。

至于 Init 方法,您是否指的是 构造函数

如果是这样,那么看看上面例子中的析构函数;构造函数(或初始化程序)可以用相同的方式定义,减去波形符,并且通常加上显式访问修饰符(publicprivate 等),例如:

public class Customer
{
    public Customer()
    {

    }
}

For classes that should be disposable, by exposing a public Dispose method, the IDispsable interface must be implemented for 'disposability' to be effective out of the scope of explicit user disposal. This has been covered many times in many places, including here, for example:

public class Customer : IDisposable
{
    public void Dispose()
    {
        Dispose(true);
        GC.SupressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            //dispose of managed resources
        }
        //dispose of unmanaged resources
    }

    ~Customer()
    {
        Dispose(false);
    }
}

Note that the destructor (the method starting with the tilde ~) may not be necessary, but read the details from the answer I linked above for clarity on the situation of what and why - this just answers your question directly.

As for an Init method, are you referring to a constructor?

If so, then look at the destructor in the above example; a constructor (or initialiser) can be defined in the same way minus the tilde and, generally, plus an explicit access modifier (public, private, et cetera), for example:

public class Customer
{
    public Customer()
    {

    }
}
晚风撩人 2024-11-07 21:24:04

您可以根据需要使用 Init 和 Dispose 方法创建一个基类,然后让其他类继承它。例如:

public class BaseClass
{
    public void Init()
    {
        //Some code
    }

    public void Dispose()
    {
        //Some code
    }
}

public class Customer : BaseClass
{
    //Some code
}

这可能对你有帮助。

You can create a base class with the Init and Dispose method as you wish and then make the other classes to inherit from it. For example:

public class BaseClass
{
    public void Init()
    {
        //Some code
    }

    public void Dispose()
    {
        //Some code
    }
}

public class Customer : BaseClass
{
    //Some code
}

That might help you.

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