Is it possible to force the use of “using” for disposable classes?

前端 未结 10 642
轮回少年
轮回少年 2020-12-05 06:49

I need to force the use of \"using\" to dispose a new instance of a class.

public class MyClass : IDisposable
{
   ...
}

using(MyClass obj = new MyClass())          


        
10条回答
  •  我在风中等你
    2020-12-05 07:23

    No it is not possible. Now what you can do is call the dispose method in the finalizer of the class (and then you can suppress the use of it if they do actually call the dispose method). That way it will fire if not done explicitly in code.

    This link will show you how to implement the finalizer / dispose pattern:

    http://www.devx.com/dotnet/Article/33167

提交回复
热议问题