Visual Studio 2015 IDisposable模式与FXCop代码分析

时间:2016-08-04 19:01:22

标签: visual-studio-2015 idisposable fxcop

最近更新到Visual Studio 2015,我尝试使用生成以下代码的内置IDisposable实现:

    #region IDisposable Support
    private bool disposedValue = false; // To detect redundant calls

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // TODO: dispose managed state (managed objects).
            }

            // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
            // TODO: set large fields to null.

            disposedValue = true;
        }
    }

    // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
    // ~MyClass() {
    //   // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
    //   Dispose(false);
    // }

    // This code added to correctly implement the disposable pattern.
    public void Dispose()
    {
        // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
        Dispose(true);
        // TODO: uncomment the following line if the finalizer is overridden above.
        // GC.SuppressFinalize(this);
    }
    #endregion

由于我的类不包含非托管资源,按照这些说明我没有覆盖终结器,反过来也没有取消注释GC.SuppressFinalize(this);线。

但是,当我运行代码分析时,我收到以下警告:

警告CA1816更改' MyClass.Dispose()'调用' GC.SuppressFinalize(object)'。这将阻止引入终结器的派生类型需要重新实现IDisposable'叫它。

那么我应该遵循哪些建议呢? FXCop,或Visual Studio 2015的内置IDisposable模式?

0 个答案:

没有答案