错误:不要覆盖 object.Finalize。相反,提供一个析构函数

发布于 2024-08-02 20:58:06 字数 1138 浏览 4 评论 0原文

在以下代码中出现上述错误。如何纠正它。谢谢。 查找。

protected override void Finalize() {     Dispose(false); } 

请在下面的代码中

using Microsoft.Win32; 
using System.Runtime.InteropServices; 

public class Kiosk : IDisposable 
{ 

    #region "IDisposable" 

    // Implementing IDisposable since it might be possible for 
    // someone to forget to cause the unhook to occur. I didn't really 
    // see any problems with this in testing, but since the SDK says 
    // you should do it, then here's a way to make sure it will happen. 

    public void Dispose() 
    { 
        Dispose(true); 
        GC.SuppressFinalize(this); 
    } 

    protected virtual void Dispose(bool disposing) 
    { 
        if (disposing) { 
        } 
        // Free other state (managed objects). 
        if (m_hookHandle != 0) { 
            UnhookWindowsHookEx(m_hookHandle); 
            m_hookHandle = 0; 
        } 
        if (m_taskManagerValue > -1) { 
            EnableTaskManager(); 
        } 
    } 

    protected override void Finalize() 
    { 
        Dispose(false); 
    } 

    #endregion 
} 

Getting the above error in following code. How to rectify it. Thanks.
Please look for

protected override void Finalize() {     Dispose(false); } 

in the below code.

using Microsoft.Win32; 
using System.Runtime.InteropServices; 

public class Kiosk : IDisposable 
{ 

    #region "IDisposable" 

    // Implementing IDisposable since it might be possible for 
    // someone to forget to cause the unhook to occur. I didn't really 
    // see any problems with this in testing, but since the SDK says 
    // you should do it, then here's a way to make sure it will happen. 

    public void Dispose() 
    { 
        Dispose(true); 
        GC.SuppressFinalize(this); 
    } 

    protected virtual void Dispose(bool disposing) 
    { 
        if (disposing) { 
        } 
        // Free other state (managed objects). 
        if (m_hookHandle != 0) { 
            UnhookWindowsHookEx(m_hookHandle); 
            m_hookHandle = 0; 
        } 
        if (m_taskManagerValue > -1) { 
            EnableTaskManager(); 
        } 
    } 

    protected override void Finalize() 
    { 
        Dispose(false); 
    } 

    #endregion 
} 

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

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

发布评论

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

评论(3

柠檬心 2024-08-09 20:58:06

Finalize() 是一种特殊方法,您无法在代码中重写。请改用析构函数语法:

~Kiosk() 
{ 
    Dispose(false); 
} 

Finalize() is a special method that you can't override in code. Use the destructor syntax instead:

~Kiosk() 
{ 
    Dispose(false); 
} 
命硬 2024-08-09 20:58:06

按它说的去做。而不是:

protected override void Finalize() 
{ 
    Dispose(false); 
} 

有:

~Kiosk () 
{ 
    Dispose(false); 
} 

Do what it says. Instead of:

protected override void Finalize() 
{ 
    Dispose(false); 
} 

Have:

~Kiosk () 
{ 
    Dispose(false); 
} 
白芷 2024-08-09 20:58:06

在 C# 中,以下语法将完全编译为您想要完成的任务。

~Kiosk()
{
    Dispose(false);
}

In C#, the following syntax compiles to exactly what you're trying to accomplish.

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