[英]How to dispose of a class with properties that do not implement IDisposable?
Not sure how to go about disposing this class as I need to free up resources due to a memory leak in the application causing it to slow down. 不确定如何处理此类,因为由于应用程序中的内存泄漏导致其速度变慢,我需要释放资源。 I am not sure how to go about disposing the class below as some of the properties don't implement IDisposable. 我不确定如何处置下面的类,因为某些属性未实现IDisposable。 I am fairly new to c# so try not to over complicate responses. 我对C#还是比较陌生,所以请尽量不要使响应过于复杂。
public class CellItem: IDisposable
{
private Timer foo = new Timer();
public int MedicationDispenseId { get; set; }
public Enumerations.Timeslot Timeslot { get; set; }
public DateTime DateAdministered { get; set; }
protected override void Dispose(bool disposing)
{
disposing = true;
Dispose(disposing);
}
}
There's a bug in the code in that: 代码中有一个错误:
protected override void Dispose(bool disposing)
{
disposing = true;
Dispose(disposing);
}
is recursive and if just going to sit there for a few moments before running out of stack space. 是递归的,如果只是在堆空间用完之前要坐在那里一会儿。
To answer you're question: If it's your code then just change the Dispose
method to release the appropriate resources. 要回答您的问题:如果是您的代码,则只需更改Dispose
方法以释放适当的资源。 If it's not then you'll have to ask whoever wrote it to fix it or think about writing your own (bug free) version. 如果不是,那么您将必须询问谁编写了它来修复它,或者考虑编写自己的版本(无错误)。
protected override void Dispose(bool disposing)
You didn't implement the IDisposable.Dispose() method so this code won't compile. 您没有实现IDisposable.Dispose()方法,因此该代码无法编译。 The protected Dispose(bool) method is an artifact of the disposable pattern. 受保护的Dispose(bool)方法是一次性模式的伪像。 It is only used when your class has a finalizer or when your class derives from a base class that implements the disposable pattern. 仅当您的类具有终结器,或者您的类是从实现了一次性模式的基类派生时,才使用它。 Neither is the case. 情况并非如此。
So keep it simple and just implement Dispose(): 因此,保持简单,只需实现Dispose()即可:
public void Dispose()
{
foo.Dispose();
}
Try reading these resources to get you started: 尝试阅读以下资源以开始使用:
IDisposable 一次性
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx http://msdn.microsoft.com/zh-CN/library/system.idisposable.aspx
Using Keyword 使用关键字
http://msdn.microsoft.com/en-us/library/yh598w02.aspx http://msdn.microsoft.com/en-us/library/yh598w02.aspx
The Disposing
flag is not a field which says whether the disposal has started for the class, but should instead be regarded as a dummy parameter to which the value true
should be passed when the protected virtual
method is called from the parameterless Dispose
method which implements the interface. Disposing
标志不是一个字段,该字段说明是否已开始处理该类,而是应将其视为一个伪参数,当从实现该参数的无参数Dispose
方法中调用protected virtual
方法时,应将true
值传递给该伪参数。接口。 The parameter was originally designed so as to allow a common "patch point" for derived classes which want to add functionality to both Dispose
and Finalize
(destructor) methods, but in practice it's almost never appropriate for a derived or unsealed class to implement Finalize
code unless the class is derived directly from Object
or from a class whose whole purpose centers around such cleanup . 该参数最初的设计目的是为希望向Dispose
和Finalize
(析构函数)方法添加功能的派生类提供通用的“补丁点”,但实际上,派生或未密封的类实现Finalize
代码几乎是不合适的。 除非该类直接从Object
派生,或者从整个目的围绕此类清除的类派生 。
Note that unlike most interfaces, the IDisposable
"contract" doesn't impose any obligations on the class which implements it , but instead exists as a standard means via which many types of classes can impose certain transferable contractual obligations on code which requests their construction. 请注意,与大多数接口不同, IDisposable
“合同”并未对实现它的类强加任何义务,而是作为一种标准手段存在,许多类型的类可通过该标准对要求其构造的代码强加某些可转让的合同义务。 A typical IDisposable
object will ask some other entity to do something on its behalf until further notice, will promise that other entity that it will be informed when its services are no longer required, and will use its Dispose
method for the purpose of giving such notice. 一个典型的IDisposable
对象将要求其他实体代表它做某事,直到另行通知为止,并承诺将在不再需要其服务时通知其他实体,并使用其Dispose
方法来发出此类通知。 。 The constructor contract for many classes which implement IDisposable
will require that the caller either ensure that before it abandons the object it will either Dispose
it or give it to some other entity that promises to do so. 许多实现IDisposable
类的构造函数协定都要求调用者确保在放弃对象之前将其Dispose
或将其交给承诺这样做的其他实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.