0
這是我創建的一個類。它目前返回一個異常,說明它處於循環中 - 現在這很明顯。一類中的一次性用品
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);
}
}
據我所知,在這裏只有兩個(技術上)一次性對象是字符串和列表。它是否正確?或者,是否有更多,更少或其他項目完全可以處理?此外,什麼特別使他們「一次性」的對象?它是否是我已經實例化的單個對象的事實?
請發佈相關代碼 – Tuco
不,您在dirSearch類中沒有IDisposable字段。 'String'和'List'不實現'IDisposable'。 –
一次性對象實現IDisposable。這是不能等待垃圾收集器被銷燬的對象(例如文件句柄)所需要的。 – Domysee