课堂上的一次性用品

时间:2015-10-19 12:00:50

标签: c# .net dispose idisposable

这是我创建的一个类。它当前返回一个异常,表明它处于循环中 - 现在这很明显。

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);
    }
}

据我所知,这里只有两个(技术上)一次性物品是字符串和列表。它是否正确?或者,我会处理更多,更少或其他物品吗?此外,具体是什么使他们&#34;一次性&#34;对象?事实上它们是我实例化的单个对象吗?

1 个答案:

答案 0 :(得分:1)

通常我们在要处置(即非托管资源,如文件,RDBMS连接,其他IDisposable实例等时实现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用作占位符,如果我理解您是基类进行搜索,那么您更改{{1}进入类似DirSearch的内容并将其设为BaseSearch