使用语法触发调用所有实现Dispose方法的对象
是否可以使用using语法来触发所有实现的对象 IDisposable接口调用对应的Dispose()方法?
例如,如果感兴趣的部分中存在ObjectOne和ObjectTwo对象,我们是否可以设置这样两个对象的Dispose()方法都会被自动调用。如示例所示,我知道如何对一种类类型执行此操作,但我不知道如何对多个类类型执行这种技巧。因为 C# 中不允许使用以下语法。
// 这不是有效的 C# 语句
using( ObjectOne one = new ObjectOne();
OjbectTwo two = new ObjectTwo() )
{
...
// hopefully, the Dispose methods of both **one** and **two** will be called.
}
具体示例说明如何仅针对一种类类型触发自动调用 Dispose 方法。
namespace ConsoleApplication1
{
class ObjectOne : IDisposable
{
public string ObjectName { get; set; }
public ObjectOne() : this("Empty") { }
public ObjectOne(string objName)
{
ObjectName = objName;
}
public void Dispose()
{
Console.WriteLine("ObjectOne.Dispose " + ObjectName);
}
}
class ObjectTwo : IDisposable
{
public string ObjectTwoName { get; set; }
public ObjectTwo() { }
public ObjectTwo(string objName)
{
ObjectTwoName = objName;
}
public void Dispose()
{
Console.WriteLine("ObjectTwo.Dispose " + ObjectTwoName);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("before");
// make sure that the Dispose method of object one1 and one2 is called
using (ObjectOne one1 = new ObjectOne(),
one2 = new ObjectOne()
)
{
// section of interest
Console.WriteLine("middle");
}
Console.WriteLine("after");
Console.ReadLine();
}
}
}
谢谢。
Is it possible to use using syntax to trigger all objects that implement
IDisposable interface call the corresponding Dispose() method?
For example, if the objects of ObjectOne and ObjectTwo exist in the section of interest, can we make it the way so that the Dispose() method of both objects will be called automatically. As the example shown, I knew how to do it for one Class type, but I don't know how to do this kind of trick for more than one Class type. Because the following syntax is not allowed in C#.
// This is not a valid C# statement
using( ObjectOne one = new ObjectOne();
OjbectTwo two = new ObjectTwo() )
{
...
// hopefully, the Dispose methods of both **one** and **two** will be called.
}
A concrete example to illustrate how to trigger the auto-calling Dispose method for just one class type.
namespace ConsoleApplication1
{
class ObjectOne : IDisposable
{
public string ObjectName { get; set; }
public ObjectOne() : this("Empty") { }
public ObjectOne(string objName)
{
ObjectName = objName;
}
public void Dispose()
{
Console.WriteLine("ObjectOne.Dispose " + ObjectName);
}
}
class ObjectTwo : IDisposable
{
public string ObjectTwoName { get; set; }
public ObjectTwo() { }
public ObjectTwo(string objName)
{
ObjectTwoName = objName;
}
public void Dispose()
{
Console.WriteLine("ObjectTwo.Dispose " + ObjectTwoName);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("before");
// make sure that the Dispose method of object one1 and one2 is called
using (ObjectOne one1 = new ObjectOne(),
one2 = new ObjectOne()
)
{
// section of interest
Console.WriteLine("middle");
}
Console.WriteLine("after");
Console.ReadLine();
}
}
}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以嵌套使用语句:
编辑:唯一的其他方法是实现 IDisposables 的自定义列表:
You can nest using statements:
EDIT: the only other way would be to implement a custom list of IDisposables:
这将在代码块之后调用两个对象的 dispose。
This will call dispose on both objects after the code block.
看看我之前问过的问题
我可以在 C# *using* 块中拥有不同类型的对象吗?
以上是处理相同类型的多个对象的方法。正如上面问题所回答的,您还可以处理不同类型的多个对象。
Take a look at the question I asked some time back
Can i have different type of objects in a C# *using* block?
The above is how you handle multiple objects of same type. As answered in the above question, you can also handle multiple objects of different types as well.
根据此:
这是您要找的吗?
Edit: After testing your code, I see the problem. You can't do it with multiple types. So what about something like this:
这本质上是将一个嵌套在另一个中使用。
注意:在我的控制台中输出:
According to this:
Is this what you're looking for?
Edit: After testing your code, I see the problem. You can't do it with multiple types. So what about something like this:
This essentially nests one using in the other.
Note: In my console this outputs: