Does PrincipalSearchResult automatically dispose all elements in its collection?

后端 未结 2 1379
遥遥无期
遥遥无期 2020-12-14 21:19

Can\'t find anything in the MSDN documentation on this.

I.e. is it enough to do, say:

using(PrincipalSearcher searcher = ...)
{
    foreach (var prin         


        
2条回答
  •  误落风尘
    2020-12-14 21:36

    I originally came to the site to ask the same question, but seeing your question gave me the motivation to break out ILSpy and figure out myself if it does do it.

    First The dispose function of the search result:

    // System.DirectoryServices.AccountManagement.PrincipalSearchResult
    public void Dispose()
    {
        if (!this.disposed)
        {
            if (this.resultSet != null)
            {
                lock (this.resultSet)
                {
                    this.resultSet.Dispose();
                }
            }
            this.disposed = true;
        }
    }
    

    From there I checked resultSet.Dispose() (in my case resultSet was a ADDNLinkedAttrSet)

    // System.DirectoryServices.AccountManagement.ADDNLinkedAttrSet
    public override void Dispose()
    {
        try
        {
            if (!this.disposed)
            {
                if (this.primaryGroupMembersSearcher != null)
                {
                    this.primaryGroupMembersSearcher.Dispose();
                }
                if (this.queryMembersResults != null)
                {
                    this.queryMembersResults.Dispose();
                }
                if (this.currentMembersSearcher != null)
                {
                    this.currentMembersSearcher.Dispose();
                }
                if (this.memberSearchResults != null)
                {
                    this.memberSearchResults.Dispose();
                }
                if (this.memberSearchersQueue != null)
                {
                    foreach (DirectorySearcher directorySearcher in this.memberSearchersQueue)
                    {
                        directorySearcher.Dispose();
                    }
                    this.memberSearchersQueue.Clear();
                }
                IDisposable disposable = this.members as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
                IDisposable disposable2 = this.membersEnum as IDisposable;
                if (disposable2 != null)
                {
                    disposable2.Dispose();
                }
                if (this.membersQueue != null)
                {
                    foreach (IEnumerable enumerable in this.membersQueue)
                    {
                        IDisposable disposable3 = enumerable as IDisposable;
                        if (disposable3 != null)
                        {
                            disposable3.Dispose();
                        }
                    }
                }
                if (this.foreignGroups != null)
                {
                    foreach (GroupPrincipal groupPrincipal in this.foreignGroups)
                    {
                        groupPrincipal.Dispose();
                    }
                }
                this.disposed = true;
            }
        }
        finally
        {
            base.Dispose();
        }
    }
    

    You can see the foreach loops where it is itterating over all of the members it has. So it is doing the Dispose for you on each member.

    So, yes it does dispose all of the members, and then some.

提交回复
热议问题