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

发布于 2024-07-24 16:53:53 字数 231 浏览 7 评论 0原文

我想知道是否有某种备忘单可以说明哪些对象与 using 语句配合良好...SQLConnectionMemoryStream 等。

更进一步,它甚至展示其他“拼图的碎片”也会很棒,例如您应该如何在结束 using 语句括号之前实际调用 connection.Close()

存在这样的东西吗? 如果没有,也许我们应该做一个。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

冬天旳寂寞 2024-07-31 16:53:53

也许可以浏览一下我关于此的帖子 http://www.lancemay.com/ 2010/01/idisposable-cheat-sheet/。 不确定这是否是您正在寻找的内容,但根据最初的问题,听起来可能是这样。

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.

回首观望 2024-07-31 16:53:53

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

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

岁月静好 2024-07-31 16:53:53

以下 C# 方法将列出在某个程序集中找到的所有 IDisposable 类型。 (使用的命名空间: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);
  }

以下 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);

  }

我不确定所有一次性列表是否非常有用,但我使用类似的代码来查找其他有趣的东西,例如 .NET 支持的文本编码的完整列表框架。

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.

不喜欢何必死缠烂打 2024-07-31 16:53:53

如果您不确定某个类是否实现了 IDisposable,请将其包含在 using 块中。 如果出现编译错误,只需将其删除即可。 您只会损失几秒钟的打字时间。

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.

仲春光 2024-07-31 16:53:53

除了其他答案之外,请注意,一个类可能实现 IDisposable,但智能感知列表中不会出现 Dispose。

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

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 */
    }
}
岛徒 2024-07-31 16:53:53

如果您使用的是 Visual Studio,您可以在类型声明上按 F12,它将带您进入元数据屏幕或类定义(如果您有源代码)。 如果您的键绑定不同,请右键单击并“转到定义”。 从那里你可以看到一个类实现了什么。 我建议您第一次遇到所有类时都这样做,以“感觉”该类可以做什么。

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.

﹂绝世的画 2024-07-31 16:53:53

获取实现 IDisposable 的类型列表的一个简单方法是打开 Reflector,导航到 System.IDisposable,展开节点,然后展开“派生类型”节点。

为了确保您的列表完整,请验证您正在使用的所有程序集是否已在 Reflector 中“打开”。

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.

帅冕 2024-07-31 16:53:53

使用 ReSharper,您可以显示所有派生类型。 也许您也可以使用对象浏览器而不使用 ReSharper 来完成此操作。 转到接口定义并查找“显示继承者”。

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

很酷又爱笑 2024-07-31 16:53:53

检查类型是否实现 IDisposable 的一种快速而肮脏的方法是创建它的实例并检查该实例是否具有 Dispose() 成员函数。 如果确实如此,那么您可以 99% 确定它实现了 IDisposable。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文