简体   繁体   English

如何处置具有无法实现IDisposable的属性的类?

[英]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();
    }

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 . 该参数最初的设计目的是为希望向DisposeFinalize (析构函数)方法添加功能的派生类提供通用的“补丁点”,但实际上,派生或未密封的类实现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.

相关问题 如果没有Dispose方法,这个类如何实现IDisposable? - How does this class implement IDisposable if it doesn't have a Dispose method? 当我的控制器的字段实现IDisposable时,该如何处置? - How do I dispose of my Controllers when their fields implement IDisposable? 如何处置实现IDiposable的对象以及在其上调用的属性/方法的类型实现IDisposable - how to dispose off objects that implement IDiposable and also the types of properties/methods invoked on them implement IDisposable 当我将它传递给IDisposable类时,我需要Dispose Stream吗? - Do i need to Dispose Stream when i Pass it to IDisposable class? 如何在实现 IDisposable 接口的 class 中使用局部变量处理对象 - How to dispose objects with local variables within a class that implements IDisposable Interface 如果using语句抛出异常,我如何处置IDisposable对象? - How do I dispose an IDisposable object if the using statement throws an exception? 如何在继承类中实现处置 - How to Implement Dispose In Inheriting Class 如何在我的班级中正确实现Dispose()? - How do I implement Dispose() in my class correctly? 处理所有实现IDisposable的嵌套对象 - Dispose of nested objects which all implement IDisposable 实施Dispose但不实施IDisposable背后的原因? - Reasoning behind implementing Dispose but not implement IDisposable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM