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

前端 未结 9 2376
挽巷
挽巷 2020-12-17 15:10

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

相关标签:
9条回答
  • 2020-12-17 15:36

    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.

    0 讨论(0)
  • 2020-12-17 15:38

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

    0 讨论(0)
  • 2020-12-17 15:38

    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".

    0 讨论(0)
  • 2020-12-17 15:49

    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.

    0 讨论(0)
  • 2020-12-17 15:58

    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.

    0 讨论(0)
  • 2020-12-17 15:59

    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.

    0 讨论(0)
提交回复
热议问题