class 中的一次性物品
Disposable items in a class
这是我创建的 class。它目前 returns 一个异常,表明它处于循环中 - 这现在很明显了。
public class dirSearch : IDisposable
{
private bool disposed = false;
public bool searchSuccessful;
public string errStr;
List<string> resList = new List<string>();
public void getEmpDetails(string filStr, string varStr)
{
string strServerDNS = "ldap.<redacted>.com:389";
string strSearchBaseDN = "ou=People,o=<redacted>.com";
string strLDAPPath = "LDAP://" + strServerDNS + "/" + strSearchBaseDN;
DirectoryEntry objDirEntry = new DirectoryEntry(strLDAPPath, null, null, AuthenticationTypes.Anonymous);
DirectorySearcher searcher = new DirectorySearcher(objDirEntry);
SearchResultCollection results;
searcher.Filter = "(uid=" + filStr + ")";
//make sure the order of the search is like so:
//UID
//empnum
//full name
searcher.PropertiesToLoad.Add(varStr);
try
{
results = searcher.FindAll();
foreach (SearchResult result in results)
{
string temStr = result.Properties[varStr][0].ToString();
resList.Add(temStr);
searchSuccessful = true;
}
}
catch (Exception e)
{
errStr = e.ToString();
searchSuccessful = false;
}
}
public void getEmpDetails(string uid)
{
string strLDAPServerAndPort = "ldap.<redacted>.com";
string strDNPrefix = "uid=" + uid + ", ";
string strLDAPContainer = "ou=people, o=<redacted>.com";
string strLDAPPath = "LDAP://" + strLDAPServerAndPort + "/" + strDNPrefix + strLDAPContainer;
DirectoryEntry objDirEntry = new DirectoryEntry(strLDAPPath, null, null, AuthenticationTypes.Anonymous);
DirectorySearcher searcher = new DirectorySearcher(objDirEntry);
SearchResultCollection results;
searcher.Filter = "(uid=" + uid + ")";
searcher.PropertiesToLoad.Add("uid");
//need conditions here for searching for more than one value, such as <redacted>Manager etc
try
{
results = searcher.FindAll();
foreach (SearchResult result in results)
{
string temStr = result.Properties["uid"][0].ToString();
resList.Add(temStr);
searchSuccessful = true;
}
}
catch (Exception e)
{
errStr = e.ToString();
searchSuccessful = false;
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (errStr != null)
{
Dispose();
}
}
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
}
据我所知,这里仅有的两个(技术上)一次性对象是字符串和列表。这个对吗?或者,是否有更多、更少或完全由我处理的其他物品?此外,是什么让它们成为 "Disposable" 对象?它们是我实例化的单个对象吗?
通常,当我们有要处理的东西(即非托管资源,如文件、RDBMS 连接、其他IDisposable
个实例等)。从技术上讲,实现可能是这样的:
// Now IDisposable is redundant: there're no fields to dispose
public class DirSearch : IDisposable {
// All these three fields don't implement iDisposable thus they can't be disposed
//TODO: change this field into (read-only) property
public bool searchSuccessful;
//TODO: change this field into (read-only) property
public string errStr;
List<string> resList = new List<string>();
// I've omitted some code
...
// Property: you may want to know if the instance has been dispose or not
public Boolean IsDisposed {
get;
protected set; // or even "private"
}
// "protected virtual" since this method should be able to be overridden in child classes
protected virtual Dispose(Boolean disposing) {
if (IsDisposed)
return;
if (disposing) {
//TODO: Dispose unmanaged resources here
// NO Dispose() call here! Beware Stack overflow
}
IsDisposed = true;
}
public Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
}
然而,更多自然是从当前DirSearch
实现中删除所有 IDisposable
内容。如果你想使用 DirSearch
作为 占位符 ,如果我理解你是 base class 进行搜索,你宁愿将 DirSearch
更改为 BaseSearch
之类的内容并使其成为 abstract
.
这是我创建的 class。它目前 returns 一个异常,表明它处于循环中 - 这现在很明显了。
public class dirSearch : IDisposable
{
private bool disposed = false;
public bool searchSuccessful;
public string errStr;
List<string> resList = new List<string>();
public void getEmpDetails(string filStr, string varStr)
{
string strServerDNS = "ldap.<redacted>.com:389";
string strSearchBaseDN = "ou=People,o=<redacted>.com";
string strLDAPPath = "LDAP://" + strServerDNS + "/" + strSearchBaseDN;
DirectoryEntry objDirEntry = new DirectoryEntry(strLDAPPath, null, null, AuthenticationTypes.Anonymous);
DirectorySearcher searcher = new DirectorySearcher(objDirEntry);
SearchResultCollection results;
searcher.Filter = "(uid=" + filStr + ")";
//make sure the order of the search is like so:
//UID
//empnum
//full name
searcher.PropertiesToLoad.Add(varStr);
try
{
results = searcher.FindAll();
foreach (SearchResult result in results)
{
string temStr = result.Properties[varStr][0].ToString();
resList.Add(temStr);
searchSuccessful = true;
}
}
catch (Exception e)
{
errStr = e.ToString();
searchSuccessful = false;
}
}
public void getEmpDetails(string uid)
{
string strLDAPServerAndPort = "ldap.<redacted>.com";
string strDNPrefix = "uid=" + uid + ", ";
string strLDAPContainer = "ou=people, o=<redacted>.com";
string strLDAPPath = "LDAP://" + strLDAPServerAndPort + "/" + strDNPrefix + strLDAPContainer;
DirectoryEntry objDirEntry = new DirectoryEntry(strLDAPPath, null, null, AuthenticationTypes.Anonymous);
DirectorySearcher searcher = new DirectorySearcher(objDirEntry);
SearchResultCollection results;
searcher.Filter = "(uid=" + uid + ")";
searcher.PropertiesToLoad.Add("uid");
//need conditions here for searching for more than one value, such as <redacted>Manager etc
try
{
results = searcher.FindAll();
foreach (SearchResult result in results)
{
string temStr = result.Properties["uid"][0].ToString();
resList.Add(temStr);
searchSuccessful = true;
}
}
catch (Exception e)
{
errStr = e.ToString();
searchSuccessful = false;
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (errStr != null)
{
Dispose();
}
}
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
}
据我所知,这里仅有的两个(技术上)一次性对象是字符串和列表。这个对吗?或者,是否有更多、更少或完全由我处理的其他物品?此外,是什么让它们成为 "Disposable" 对象?它们是我实例化的单个对象吗?
通常,当我们有要处理的东西(即非托管资源,如文件、RDBMS 连接、其他IDisposable
个实例等)。从技术上讲,实现可能是这样的:
// Now IDisposable is redundant: there're no fields to dispose
public class DirSearch : IDisposable {
// All these three fields don't implement iDisposable thus they can't be disposed
//TODO: change this field into (read-only) property
public bool searchSuccessful;
//TODO: change this field into (read-only) property
public string errStr;
List<string> resList = new List<string>();
// I've omitted some code
...
// Property: you may want to know if the instance has been dispose or not
public Boolean IsDisposed {
get;
protected set; // or even "private"
}
// "protected virtual" since this method should be able to be overridden in child classes
protected virtual Dispose(Boolean disposing) {
if (IsDisposed)
return;
if (disposing) {
//TODO: Dispose unmanaged resources here
// NO Dispose() call here! Beware Stack overflow
}
IsDisposed = true;
}
public Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
}
然而,更多自然是从当前DirSearch
实现中删除所有 IDisposable
内容。如果你想使用 DirSearch
作为 占位符 ,如果我理解你是 base class 进行搜索,你宁愿将 DirSearch
更改为 BaseSearch
之类的内容并使其成为 abstract
.