ASP.NET:实现 Init 和 Dispose 方法
ASP.NET Web 应用程序是否可以只有一个 Init
和一个 Dispose
方法,或者我可以为每个我想要关联这些方法的类实现这些方法吗?
更具体地说,我有 Customer
组件和 CustomerRecord
类,并且希望在这两个组件中实现 Init
和 Dispose
方法他们。
执行此操作的正确方法是什么?
要求:
我希望为每个上述类拥有独立的 Init
和 Dispose
方法。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于应该可处置的类,通过公开公共
Dispose
方法,必须实现IDispsable
接口,以使“可处置性”在显式用户处置范围之外有效。这已经在很多地方被多次提及,包括这里,例如:请注意,析构函数(以波形符
~
开头的方法)可能不是必需的,但请阅读我上面链接的答案中的详细信息,以清楚地了解什么的情况和为什么 - 这只是回答你的直接提问。至于
Init
方法,您是否指的是 构造函数?如果是这样,那么看看上面例子中的析构函数;构造函数(或初始化程序)可以用相同的方式定义,减去波形符,并且通常加上显式访问修饰符(
public
、private
等),例如:For classes that should be disposable, by exposing a public
Dispose
method, theIDispsable
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: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:您可以根据需要使用 Init 和 Dispose 方法创建一个基类,然后让其他类继承它。例如:
这可能对你有帮助。
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:
That might help you.