不调用Disposed()

时间:2015-08-18 11:51:16

标签: breakpoints dispose

我有IDisposable Business class:   私人o_Entities上下文;

    public o_Entities GetContext()
    {
        if (context == null)
            context = new o_Entities();

        return context;
    }


 public List<SModel> List(int s_id)
        {
            var currentContext = GetContext();
            var s = (from c in currentContext.tS
                         where (s_id == 0 || s_id == null || (c.s_id == s_id))
                         orderby c.s_id ascending
                         select new SModel
                         {
                             s_id = c.s_id,
                             s_name = c.s_name,
                             address = c.address,
                             phone = c.phone.Trim()
                         });

            return s.ToList();
}

private bool dispos = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                context.Dispose();
            }
        }
        this.disposed = true;
    }

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

我在Dispose()函数上设置了断点,但从未调用它。有什么不对吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

不会自动调用

Dispose()(它不像C ++中的析构函数)。当您不再需要该对象时,您有责任调用它。

有关详细信息,请参阅this question