Is there a way for a class that implements IDisposable to throw an exception if it's not been instantiated via a using block?

I spotted some potentially dangerous classes that would be much less dangerous if they couldn't be instantiated unless they were done so within a using statement.

I'm wondering if there's a way to force the classes to only be instantiated in that way.

(I'm aware what the IL compiles out as which is why I'm not massively sure this is even possible)

Cheers,

Phil.

15
задан mkj 28 October 2016 в 22:29
поделиться