2015-10-19 27 views
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); 
    } 
} 

據我所知,在這裏只有兩個(技術上)一次性對象是字符串和列表。它是否正確?或者,是否有更多,更少或其他項目完全可以處理?此外,什麼特別使他們「一次性」的對象?它是否是我已經實例化的單個對象的事實?

+1

請發佈相關代碼 – Tuco

+1

不,您在dirSearch類中沒有IDisposable字段。 'String'和'List '不實現'IDisposable'。 –

+0

一次性對象實現IDisposable。這是不能等待垃圾收集器被銷燬的對象(例如文件句柄)所需要的。 – Domysee

回答

1

通常我們實現IDisposable當我們有東西處置(即非託管資源文件一樣,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); 
    } 
} 

然而,更多的自然刪除所有IDisposable東西從目前DirSearch實現。如果你想使用DirSearch作爲佔位,如果我理解你的權利,作爲一個搜索基,你寧願更改DirSearch成類似BaseSearch並使其abstract