I'm doing some Active Directory work with .NET's System.DirectoryServices.AccountManagement
namespace. I noticed that Principal
implements IDisposable
, which causes sort of a headache since everything in that namespace inherits Principal
.
E.g. consider the following code to get all the users in a group:
PrincipalContext domain = new PrincipalContext(ContextType.Domain);
GroupPrincipal group = GroupPrincipal.FindByIdentity(domain, "MyGroup");
PrincipalSearchResult<Principal> users = group.GetMembers();
Every single type in that snippet implements IDisposable
, including all the users returned by the search and the search result set itself.
Disposing of the domain
and group
objects is not a big deal (it would be easy with a using()
block), but what do I do about every result? Do I really have to loop through that users
collection and dispose of every one?