C# 中非托管对象的处理

发布于 10-07 05:20 字数 907 浏览 7 评论 0原文

假设我有一个类,其中包含 MyCustomDatabaseAccess 作为数据成员。 MyCustomDatabaseAccess 有一个 Dispose() 方法。 MyCustomDatabaseAccess 是访问数据库的中间件类。

public class MyClass {
   private MyCustomDatabaseAccess db_access;
}

MyClass需要实现IDisposable接口吗?

我现在的解决方案是做这样的事情:

    public class MyClass {
       private MyCustomDatabaseAccess db_access;

       public void GetDBResults () {
          db_access = new MyCustomDatabaseAccess();
          DataTable dt = db_access.ExecuteStoredProc(param1, param2, etc..);

          //do stuff with results

          db_access.Dispose();

    }

}

根据我在 MSDN 上读到的内容,确保正确处置该对象的另一种方法是让 MyClass 实现 IDisposable 接口,然后实现 Dispose() 函数,然后调用它在调用 MyClass 对象的类中。 请参阅此了解更多信息 http://www.devx.com/dotnet/Article/33167/0 /page/3

哪种方式更好,为什么? 谢谢!

Let's say I have a class that has a MyCustomDatabaseAccess as a data member. MyCustomDatabaseAccess has a Dispose() method. MyCustomDatabaseAccess is the middleware class that accesses the database.

public class MyClass {
   private MyCustomDatabaseAccess db_access;
}

Does MyClass need to implement IDisposable interface?

My solution right now is to have do something like this:

    public class MyClass {
       private MyCustomDatabaseAccess db_access;

       public void GetDBResults () {
          db_access = new MyCustomDatabaseAccess();
          DataTable dt = db_access.ExecuteStoredProc(param1, param2, etc..);

          //do stuff with results

          db_access.Dispose();

    }

}

From what I read on MSDN, another way to make sure that this object is disposed of properly would be to have MyClass implement IDisposable interface, then implement a Dispose() function, then call it in the class that calls an object of MyClass.
see this for more info
http://www.devx.com/dotnet/Article/33167/0/page/3

Which way is preferable and why?
thanks!

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

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

发布评论

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

评论(4

牵你手2024-10-14 05:20:30

我建议使用以下两种方法之一:

  1. 将 IDisposable 对象包装在 using 块中。
  2. 在您的类中保留对 IDisposable 对象的引用,并让您的类实现 IDisposable。在类的 Dispose 方法中处置该对象。

您应该选择哪个取决于对象所需的生命周期。

  • 如果您仅在单个方法调用期间需要它,那么第一个选项 - using 块 - 是更好的选择,因为它更简单且更难出错。 using 块(几乎)保证一旦不再需要该对象,就会立即调用 Dispose - 即使在抛出异常的情况下也是如此。
  • 如果一次性对象的持续时间必须长于单个方法调用的持续时间,那么您不能使用第一个选项,因此您应该使用第二个选项 - 即您的类应该实现 IDisposable。

I would suggest using one of the following two approaches:

  1. Wrap the IDisposable object in a using block.
  2. Keep a reference to the IDisposable object inside your class and have your class implement IDisposable. Dispose the object in your class's Dispose method.

Which you should choose depends on the required lifetime of the object.

  • If you only need it for the duration of a single method call then the first option - a using block - is preferable because it is simpler and harder to get wrong. The using block (almost) guarantees that Dispose will be called for you as soon as the object is no longer needed - even in the case where an exception is thrown.
  • If the disposable object must last longer than the duration of a single method call then you cannot use the first option, so you should use the second - i.e. your class should implement IDisposable.
懒的傷心2024-10-14 05:20:30

我认为你只需要确保你的物品被处理掉。

如果你在你的方法中这样做,即使出现意外错误,那么我认为你没问题。如果您在类中保留非托管资源,那么实现 IDisposable 是一种确保您有机会在对象完成时处置资源的方法,或者为您的用户提供一种显式处置资源的方法。

如果您仅在方法中创建和使用资源,而不在类中保存引用,那么只要确保它们在方法中被释放(通过手动执行,或者更简单地通过将 then 包装在 using 块中) ),那么我想你应该没问题。

I think you just need to ensure your objects are disposed.

if you are doing this in your method, even if there is an unexpected error, then I think you are ok. If you hold on to unmanaged resources in your class then implementing IDisposable is a way to ensure that you get a chance to dispose resources when your object is finalized, or to give your users a way to dispose of the resources explicitly.

If you are only creating and using the resources in a method and not holding references in the class then as long as you are ensuring they are disposed in the method (either by doing it manually , or more easily, by wrapping then in a using block), then you should be ok I think.

朱染2024-10-14 05:20:30

如果您确实正在维护与数据库的连接(而不仅仅是您想在此处发布的示例代码),那么您现在正在做正确的事情。即打开数据库连接、查询数据并关闭/处理它。

另一方面,如果您的资源占用大量内存,但对尽快关闭/处置的时间要求不高,则应使用 IDisposable.Dispose() 方法。当您的对象不再被引用并且 GC 需要内存时,您的对象将被收集,并且 Dispose() 方法将在其上关闭,这将处理您的非托管大对象。

If you really are maintaining a connection to a database (and its not just a sample code you wanted to post here), you are doing the right thing right now. That is opening database connection, querying for data and closing/disposing it.

On the other hand, IDisposable.Dispose() method should be used if your resources take up a lot of memory but are not time critical to be closed/disposed as soon as possible. When your object is no longer referenced and GC needs memory, your object will be collected and Dispose() method will be closed on it which will dispose your unmanaged large object.

小情绪2024-10-14 05:20:30

是的,实现 IDisposable 就是您告诉客户端代码的方式“嘿,您需要对此对象调用 dispose,因为它维护对非托管资源的引用。”

Yes, implementing IDisposable is how you tell clients of your code "hey, you need to call dispose on this object because it maintains references to unmanaged resources."

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