Disposable singleton in C#

前端 未结 8 1826
Happy的楠姐
Happy的楠姐 2020-12-05 08:14

I have a singleton that uses the \"static readonly T Instance = new T();\" pattern. However, I ran into a case where T is disposable, and actually needs to be disposed for u

相关标签:
8条回答
  • 2020-12-05 09:12

    Another option to make a disposable Singleton is to use SandCastle's [Singleton] atribute for your class, then Castle framework takes care of disposing all disposable Singleton objects

    0 讨论(0)
  • 2020-12-05 09:13

    If the class implements IDisposable (as you imply it does) then just call x.Dispose()

    0 讨论(0)
提交回复
热议问题