如何配置接口对象 [英] How to dispose the interface objects

查看:57
本文介绍了如何配置接口对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何在C#中布置接口对象?




在此先谢谢您.

Hi


How to dispose the interface objects in C#?




Thanks in advance.

推荐答案

接口仅定义了合同.

从MSDN:"13.接口
Visual Studio .NET 2003

接口定义合同.实现接口的类或结构必须遵守其协定.一个接口可以从多个基本接口继承,而一个类或结构可以实现多个接口.

接口可以包含方法,属性,事件和索引器.接口本身不为其定义的成员提供实现.该接口仅指定实现该接口的类或结构必须提供的成员."


因此,您不必处置接口本身,而要处置实现它们的对象.您可能有一个看起来像这样的对象

An interface is just defines a contract.

From MSDN: "13. Interfaces
Visual Studio .NET 2003

An interface defines a contract. A class or struct that implements an interface must adhere to its contract. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.

Interfaces can contain methods, properties, events, and indexers. The interface itself does not provide implementations for the members that it defines. The interface merely specifies the members that must be supplied by classes or structs that implement the interface."


So, you don''t dispose of Interfaces per se, you dispose of the objects that implement them. You could have an object that looks like this

public class SomeDisposableObject : IMyCustomInterface, IDisposable
{
   // Implementation of IMyCustomInterface and IDisposable in here!
}



现在,您可以使用一种方法来获取此引用,如下所示



Now you could have a method that gets this reference as follows

IMyCustomInterface foo = new SomeDisposableObject ();
foo.Whatever();
((IDisposable)foo).Dispose();



不过,这不是很干净,如果我们可以在接口周围使用using块,那就更好了.在这种情况下,让您的界面继承IDisposable



That''s not very clean though, it would be nicer if we could just use a using block around the interface. In which case, have your interface inherit IDisposable

public Interface IMyCustomInterface : IDisposable
{
    void Whatever;
}



您现在可以使用like



You can now use like

using (IMyCustomInterface bar = new SomeDisposableObject())
{
    bar.Whatever();
}


这篇关于如何配置接口对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆