Am I being 'used' ?

  • Thread starter Thread starter Brett Wiltshire
  • Start date Start date
B

Brett Wiltshire

Is it possible to check whether an object (this) has been instantiated
as the target of a using block?

e.g.

public class MyClass : IDisposable
{

public MyClass()
{
if ( ! ThisObjectIntantiatedByUsingBlock() )
{
throw new ApplicationException ( "This class can only be
invoked as the target of a 'using' block." );
}
// whatever
}
public void Dispose()
{
// whatever
}
}


Since a using block expands out to a try/catch/finally I'm guessing
not, but thought I'd plumb the collective depths of your expertise,
just in case.

Cheers,
Brett.
 
Brett Wiltshire said:
Is it possible to check whether an object (this) has been instantiated
as the target of a using block?

No. One thing you *can* do is add a finalizer (in debug mode only,
perhaps) which writes an error log entry if it's ever called - and
suppress the finalizer in Dispose.
 
Ah, if only for a contractual (compiler / runtime enforced) disposable
pattern ;-p
(although the syntax to indicate transfer of ownership [e.g. as the
return from a Create method] might get too messy)

Marc
 
Thought as much. Oh well, it'll be CAPITALS IN THE DOCUMENTATION
then.

Thanks for your comments, guys.


Brett.
 
Back
Top