Is there a list of common object that implement IDisposable for the using statement?

╄→гoц情女王★ 提交于 2019-12-18 04:33:29

问题


I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection, MemoryStream, etc.

Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you should actually call connection.Close() before the closing using statement bracket.

Anything like that exist? If not, maybe we should make one.


回答1:


Microsoft FxCop has a rule checking that you use an IDisposbale in a using block.




回答2:


Perhaps glance at my post on this at http://www.lancemay.com/2010/01/idisposable-cheat-sheet/. Not sure if that's what you're looking for, but based on the original question, it sounds like it may be.




回答3:


The following C# method will list all IDisposable types found in a certain assembly. (Used namespaces: System, System.Collections.Generic, System.IO, System.Reflection)

  static void PrintDisposableTypesFromFile(String path)
  {
     Assembly assembly = Assembly.LoadFrom(path);
     foreach (Type type in assembly.GetTypes())
        if (type.GetInterface("IDisposable") != null)
           Console.WriteLine(type.FullName);
  }

The following C# method makes use of the previous one to do the same for all assemblies in a directory and its subdirectories, e.g. "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727":

  static void PrintDisposableTypesFromDirectory(DirectoryInfo dir, bool warn)
  {
     foreach (FileInfo file in dir.GetFiles("*.dll"))
     {
        try
        {
           PrintDisposableTypesFromFile(file.FullName);
        }
        catch (Exception ex)
        {
           if (warn)
           {
              Console.Error.WriteLine(
                 String.Format(
                    "WARNING: Skipped {0}: {1}",
                    new object[] { file.FullName, ex.Message }));
           }
        }
     }
     // recurse
     foreach (DirectoryInfo subdir in dir.GetDirectories())
        PrintDisposableTypesFromDirectory(subdir, warn);

  }

I'm not sure the list of all disposables is very useful, but I've used similar code to find other interesting things like the full list of text encodings supported by the .NET framework.




回答4:


If you are unsure whether a class implements IDisposable or not, enclose it in a using block regardless. If you get a compile error, just remove it. You'll only lose a few seconds typing time.




回答5:


In addition to the other answers, note that a class might implement IDisposable but not have Dispose come up in the intellisense list.

class MyClass :IDisposable
{
    void IDisposable.Dispose()
    {
        /* Stuff */
    }
}



回答6:


If you are using Visual Studio you can press F12 on a type declaration it will take you to a meta-data screen or the class definition (if you have the source code). If you keybindings are different right-click and "go to definition". From there you can see what a class implements. I suggest doing this for all classes the first time you encounter them to get a "feel" for what the class can do.




回答7:


An simple way to get a list of types that implement IDisposable is to crack open Reflector, navigate to System.IDisposable, expand the node, and then expand the 'Derived Types' node.

To be sure that your list is complete, verify that all the assemblies you're using have been 'Open'ed in Reflector.




回答8:


With ReSharper you can show all derived types. Maybe you can do it with the object browser without ReSharper, too. Go to the interface definition and look for "show inheritors".




回答9:


A quick&dirty way of checking if a type implements IDisposable is to create an instance of it and check if the instance has a Dispose() member function. If it does then you can be 99% sure that it implements IDisposable.



来源:https://stackoverflow.com/questions/1033334/is-there-a-list-of-common-object-that-implement-idisposable-for-the-using-statem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!