简体   繁体   English

是否有一个为using语句实现IDisposable的公共对象列表?

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

I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection , MemoryStream , etc. 我想知道是否有某种备忘单,其中的对象与using语句一致... SQLConnectionMemoryStream等。

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. 更进一步,甚至可以显示其他“拼图块”,就像你应该在关闭之前使用语句括号实际调用connection.Close()一样。

Anything like that exist? 这样的事情存在吗? If not, maybe we should make one. 如果没有,也许我们应该制作一个。

Perhaps glance at my post on this at http://www.lancemay.com/2010/01/idisposable-cheat-sheet/ . 也许在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. 不确定这是否是您正在寻找的,但根据原始问题,听起来可能是这样。

Microsoft FxCop有一个规则检查您在使用块中使用IDisposbale。

The following C# method will list all IDisposable types found in a certain assembly. 以下C#方法将列出在某个程序集中找到的所有IDisposable类型。 (Used namespaces: System, System.Collections.Generic, System.IO, System.Reflection) (使用的名称空间: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, eg "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727": 以下C#方法使用前一个方法对目录及其子目录中的所有程序集执行相同操作,例如“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. 我不确定所有一次性用品的列表是否非常有用,但我使用类似的代码来查找其他有趣的内容,例如.NET框架支持的文本编码的完整列表。

If you are unsure whether a class implements IDisposable or not, enclose it in a using block regardless. 如果您不确定某个类是否实现了IDisposable ,请将其封装在using块中。 If you get a compile error, just remove it. 如果出现编译错误,只需将其删除即可。 You'll only lose a few seconds typing time. 你只会输入几秒钟的打字时间。

In addition to the other answers, note that a class might implement IDisposable but not have Dispose come up in the intellisense list. 除了其他答案之外,请注意一个类可能实现IDisposable但不会在intellisense列表中出现Dispose。

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

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). 如果您使用的是Visual Studio,则可以在类型声明上按F12,它将带您进入元数据屏幕或类定义(如果您有源代码)。 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. 我建议你第一次遇到它们时为所有课程做这个,以便对课程的内容有“感觉”。

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. 获取实现IDisposable的类型列表的一种简单方法是破解打开Reflector,导航到System.IDisposable ,展开节点,然后展开“派生类型”节点。

To be sure that your list is complete, verify that all the assemblies you're using have been 'Open'ed in Reflector. 为确保列表完整,请验证您正在使用的所有程序集是否已在“反射器”中“打开”。

With ReSharper you can show all derived types. 使用ReSharper,您可以显示所有派生类型。 Maybe you can do it with the object browser without ReSharper, too. 也许你可以使用没有ReSharper的对象浏览器来完成它。 Go to the interface definition and look for "show inheritors". 转到接口定义并查找“show inheritors”。

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. 检查类型是否实现IDisposable的快速而脏的方法是创建它的实例并检查实例是否具有Dispose()成员函数。 If it does then you can be 99% sure that it implements IDisposable. 如果确实如此,那么你可以99%确定它实现了IDisposable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#的“ using”语句和自定义对象的函数时,是否需要实现IDisposable? - Using C#'s 'using' statement with a custom object's function, do I Need to implement IDisposable? 在没有 using 语句的情况下处理 IDisposable object - Handling IDisposable object without the using statement 我们可以对未实现IDisposable的对象使用Using语句吗 - Can we use Using statement for objects which does not implement IDisposable 使用语句和IDisposable接口 - Using statement and the IDisposable interface 如果using语句抛出异常,我如何处置IDisposable对象? - How do I dispose an IDisposable object if the using statement throws an exception? 在 IDisposable 对象上使用语句 - 调用 Dispose 方法的延迟 - using statement on IDisposable object - delay of calling Dispose method C#使用Statement和iDisposable - C# Using Statement and iDisposable 为什么总是需要在具有 IDisposable 成员的 object 上实现 IDisposable? - Why is it always necessary to implement IDisposable on an object that has an IDisposable member? C#/ VB.NET using语句实际上对未实现IDisposable的类做任何事情 - Does C#/VB.NET using statement actually do anything with classes that do not implement IDisposable 使用IDisposable处理对象是否正确? - Is this correct for disposing an object using IDisposable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM