本文介绍了是否PrincipalSearchResult< T>自动配置在其集合中的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找不到MSDN文档中的任何这一点。

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

即。是它足够的做,说:

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

using(PrincipalSearcher searcher = ...)
{
    foreach (var principal in searcher.FindAll())
    {
        ... do something ...
    } // The PrincipalSearchResult<T> returned by searcher.FindAll is disposed here
}

这是我见过做最例子,或者我应该做的:

which is what most examples I've seen do, or should I do:

using(PrincipalSearcher searcher = ...)
{
    foreach(var principal in searcher.FindAll())
    {
        using (principal)
        {
            // ... do something ...
        }
    } 
}

后者(显式地处理迭代中的每个项目)看起来更安全 - 即符合指引,明确处理所有IDisposable的对象 - 但它是一个有点乱;例如,precludes使用LINQ的迭代搜索结果。

The latter (explicitly disposing each item during iteration) looks "safer" - i.e. conforms to the guideline to explicitly dispose all IDisposable objects - but it's a bit messy; for example, it precludes the use of LINQ to iterate over search results.

在回应@ RUP的评论:

In response to @Rup's comment:

您可以编写返回一个结果从父迭代器

是的,我认为这工作,使LINQ。类似下面的扩展方法:

Yes, I think that would work to enable LINQ. Something like the following extension method:

public static IEnumerable<T> EnumerateAndDispose<T>(this IEnumerable<T> collection) where T : IDisposable
{
    foreach (T item in collection)
    {
        using (item)
        {
            yield return item;
        }
    }
}

可以使用的为:

which can be used as:

searcher.FindAll().EnumerateAndDispose().Select(... use LINQ ...)

不过,有必要吗?

But is it necessary?

推荐答案

一般speacking,在很多情况下,不调用Dispose()不会造成大问题:写得好处置的对象将实施清理东西的必要了同样的逻辑终结。(免责声明:我不是说不叫处理:!这是有原因的。例如,最终确定可能发生了很多后来我只说明什么后果这里)

Generally speacking, in many cases, not calling Dispose() will not cause big problems: well written disposable objects will implement the same logic needed to clean things up in the finalizer. (Disclaimer: I'm not saying "do not call dispose": it is there for a reason! For example, Finalization can happen a lot later. I'm only describing what are the consequences here).

不过,AD对象是一个明显的例外;尤其是, SearchResultCollection 是众所周知的,从这个问题的痛苦(参考:MSDN(既类文档及其他物品),和<一href="http://books.google.it/books?id=exlzxcsE-7QC&lpg=PA696&ots=JE-bIYLmG-&dq=SearchResultCollection%20dispose&hl=it&pg=PA696#v=onepage&q=SearchResultCollection%20dispose&f=false">Active目录:设计,部署和运行Active Directory )。看来,由于技术原因,不可能以释放资源在其终结,所以不调用dispose将导致内存泄漏。

However, AD objects are a notable exception; in particular, SearchResultCollection is known for suffering from this problem (references: MSDN (both the class docs and other articles), and Active Directory: Designing, Deploying, and Running Active Directory). It seems that for technical reasons it is not possible to release resources in its finalizer, so not calling dispose will lead to memory leaks.

正如斯科特和乔,很多MSDN的例子并不调用Dispose集合中的物品;然而,瑞安邓恩,前Windows Azure的技术专家与人合着的的.NET开发人员指南目录服务编程的,建议使用来调用的。从帖子:

As pointed out by Scott and Joe, many MSDN examples do not call dispose on the items in the collection; however, Ryan Dunn, former Windows Azure Technical Evangelist and co-author of the The .NET Developers Guide to Directory Services Programming, recommends to use to call dispose on each item in this blog post. From the post:

在一般情况下,始终明确以下对象类型调用Dispose():

      
  • 的DirectoryEntry
  •   
  • SearchResultCollection(从.FindAll())
  •   
  • 在DirectorySearcher从(如果您没有明确设置一个SearchRoot)
  •   
  • DirectoryEntry
  • SearchResultCollection (from .FindAll())
  • DirectorySearcher (if you have not explicitly set a SearchRoot)

这是你可以有一个权威人士最近,我相信; 然而的我个人的意见是:

This is the closest you can have to an "authoritative source", I believe; however my personal opinion is:

  • 如果可以的话,就调用dispose。它不会做任何不好的,特别是如果你能找回LINQ功能与乔的扩展方法
  • 去使用反射/ ilspy /反汇编和/或如 dotTrace 的内存配置文件,真正看到是怎么回事(基本上,斯科特已经做了,但不断深入)
  • if you can, do call dispose. It will not make any bad, especially if you can get back LINQ functionality with Joe's extension method
  • go and use reflector/ilspy/ildasm AND/OR a memory profile like dotTrace to really see what is going on (basically, what Scott already did, but going deeper)

这篇关于是否PrincipalSearchResult&LT; T&GT;自动配置在其集合中的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:38