c# - IDisposable object disposed before end of using block -
i have using
block around idisposable directoryentry
create directory entry, access 1 of properties, , dispose of it. however, directory entry being disposed prior end of using block.
public static propertyvaluecollection getproperty( principal principal, string propertyname) { using (var directoryentry = principal.getasdirectoryentry()) { return directoryentry.properties[propertyname]; } } public static directoryentry getasdirectoryentry( principal principal) { return principal.getunderlyingobject() directoryentry; }
the error thrown on return directoryentry.properties[propertyname];
line, saying directory entry disposed. can remove using block , code work, concerned object never getting disposed. call multiple times, multiple instances of directory entry being created , never disposed?
your code isn't creating directoryentry
instance, nor principal.getunderlyingobject()
method (which isn't factory method). since code doesn't manage lifetime of instance, code shouldn't disposing it.
in particular case, instance returned principal.getunderlyingobject()
stored in state of principal
instance. after dispose once, every subsequent call principal.getunderlyingobject()
on same principal
instance return same, disposed, instance.
Comments
Post a Comment