FindAll() 的 DirectorySearcher.SizeLimit = 1 等于 FindOne() [DirectoryServices/.net]
在.net中使用DirectorySearcher时,这两个语句是否相等?
两者相同:
Dim ds As New DirectorySearcher
' code to setup the searcher
第一个语句
ds.FindOne()
第二个语句
ds.SizeLimit = 1
ds.FindAll()
...显然 FindOne() 返回 SearchResult 对象而 FindAll() 返回 SearchResultCollection 对象
When using the DirectorySearcher in .net, are these two statements equal?
Same for both:
Dim ds As New DirectorySearcher
' code to setup the searcher
First statement
ds.FindOne()
Second statement
ds.SizeLimit = 1
ds.FindAll()
...except obviously that FindOne() returns a SearchResult object and FindAll() returns a SearchResultCollection object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,那几乎是一样的。
几乎,因为在 .NET 2.0 中(不确定是否已在更新的版本中修复),.FindOne() 调用存在一些内存泄漏问题,因此最佳实践是(或曾经)始终使用 .FindAll() 并迭代超过你的结果。
马克
Yes, that would be almost the same.
Almost, because in .NET 2.0 (not sure if it's been fixed in more recent versions), the .FindOne() call had some issues with leaking memory, so best practice is (or was) to always use .FindAll() and iterate over your results.
Marc
@marc_s 是对的,只是 FindOne 内存泄漏错误在 .NET 1.x 中存在,并在 .NET 2.0 中得到修复。
发生这种情况是因为 FindOne 的 .NET 1.x 实现在幕后调用 FindAll,并且并不总是处理 FindAll 返回的 SearchResultCollection:
在上面的代码中,如果集合为空(未找到结果),则不会调用 collection1.Dispose ,导致内存泄漏,如 FindAll 的 MSDN 文档。
您可以在.NET 2.0 中安全地使用 FindOne。 或者,如果您使用FindAll,则需要确保处置返回的SearchResultCollection,否则将出现相同的内存泄漏,例如:
@marc_s is right, except that the FindOne memory leak bug was in .NET 1.x and is fixed in .NET 2.0.
It happened because the .NET 1.x implementation of FindOne calls FindAll under the covers and does not always dispose the SearchResultCollection returned by FindAll:
In the above code collection1.Dispose will not be called if the collection is empty (no result is found), resulting in a memory leak as described in the remarks section of the MSDN documentation for FindAll.
You can use FindOne safely in .NET 2.0. Or if you use FindAll, you need to make sure you dispose the returned SearchResultCollection or you will have the same memory leak, e.g.: