DirectoryEntry Disposal Test

C

cameron

I am in a situation in which I am recieving a DirectoryEntry that may or
may not be already be disposed. I need a very fast, (from a code point
of view not necissarily a coding point of view), way of testing if it
has been disposed.

Currently I have:

try
{
string Junk MyDirectoryEntry.Guid.ToString();
}
catch
{
// it has been disposed of, put logic here
}

This is so painfully slow. The 1 line in the try take .00450 seconds and
that of course is not acceptable. Does anyone have a nicer way of
testing for this condition?

-Cam
 
M

Michael Giagnocavo [MVP]

Well, DirectoryEntry has a private bool field called disposed. You could use
reflection to get that out. I'd imagine that'd be faster than throwing an
exception.

And, AFAIK, if the property cache is filled, then getting the guid might not
throw an exception.

-mike
MVP
 
W

Willy Denoyette [MVP]

Basically Is see two options:
1) Implement the DirectoryEntry.Disposed eventhandler, in which you set a
flag ( f.i IsDisposed) you can check before using the DirectoryEntry object.
2) Put all your processing in a using block.

Willy.
 
W

Willy Denoyette [MVP]

The "real" cache is contained in the unmanaged code portion (ADSI COM
object) as an unmanaged memory resource, but as the DirectoryEntry object is
disposed off, so is the reference to the underlying COM object reference.
Now the (managed) property data only reflects a part of the cached ADSI
data, so it wouldn't be wise to access this data once the object is
disposed.
Therefore, System.ObjectDisposedException is thrown on you when you try to
access the DirectoryEntry object reference.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top