本文介绍了使用了IDisposable接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在关于的IDisposable

我创造,我会希望使用系统资源的大部分时间接口,但并非总是如此。难道是明智的预期用途包括的IDisposable 在我的接口?

例如我有一个接口,它提供了平均同步到。

 接口IDateTimeProvider:IDisposable接口
{
    INT LeapSeconds {获取;集;}
    日期时间LocalNow {获取;}
    日期时间UtcNow {获取;}
    日期时间GpsNow {获取;}
}

类NtpTimeProvider:IDateTimeProvider
{
    //假设客户端设置和准备使用。
    //通过网络资源的获取时间
    程序NtpClient客户端;

   NtpTimeProvider(INT leapSeconds)
   {LeapSeconds = leapSeconds;}

    INT LeapSeconds {获取;集;}
    日期时间LocalNow {{返回client.Utc};}
    日期时间UtcNow {{返回client.Utc};}
    日期时间GpsNow {{返回client.Utc  -  TimeSpan.FronSeconds(LeapSeconds);}}
    无效的Dispose()
    {
        如果(!客户端= NULL)Client.Dispose();
    }
}


类SystemTimeProvider:IDateTimeProvider
{

   SystemTimeProvider(INT leapSeconds)
   {LeapSeconds = leapSeconds;}

    INT LeapSeconds {获取;集;}
    日期时间LocalNow {{返回DateTime.Now};}
    日期时间UtcNow {{返回DateTime.UtcNow};}
    日期时间GpsNow {{返回DateTime.UtcNow  -  TimeSpan.FronSeconds(LeapSeconds);}}
    无效的Dispose()
    {//显然这是没有必要}
}
 

所以,问题是,我应该征收的时候我希望大多数实现将使用需要释放系统资源的IDisposable 要求?目前,我做到这一点,因为它是那么容易当的IDateTimeProvider的用户释放资源以及

 如果(myDateTimeProvider是IDisposable接口)((IDisposable接口)myDateTimeProvider).Dispose();
 

将不需要

解决方案

这是值得商榷的,但也有按本指南的框架的例子。一个很好的例子是 - 它实现的IDisposable 即使有子类的地方,这是没有必要的。

我会谨慎使用约但是需要你的用户这一点,除非你真的是相当肯定的是,几乎所有的实现都需要的IDisposable ,而不仅仅是其中的几个

In regards to IDisposable

I'm creating interface that I would expect to use system resources most of the time, but not always. Would it be prudent to anticipate the usage include IDisposable on my Interface?

For example I have an interface that provides a mean to synchronize to.

interface IDateTimeProvider : IDisposable
{
    int LeapSeconds {get;set;}
    DateTime LocalNow {get;}
    DateTime UtcNow {get;}
    DateTime GpsNow {get;}
}

class NtpTimeProvider : IDateTimeProvider
{
    // Assume client is setup and ready to use.
    // Obtains time via network resources
    NtpClient client;  

   NtpTimeProvider (int leapSeconds)
   { LeapSeconds = leapSeconds;}

    int LeapSeconds {get;set;}
    DateTime LocalNow {get{return client.Utc};}
    DateTime UtcNow {get{return client.Utc};}
    DateTime GpsNow {get{return client.Utc - TimeSpan.FronSeconds(LeapSeconds);}}
    void Dispose()
    {
        if(client != null) Client.Dispose();
    }
}


class SystemTimeProvider : IDateTimeProvider
{

   SystemTimeProvider (int leapSeconds)
   { LeapSeconds = leapSeconds;}

    int LeapSeconds {get;set;}
    DateTime LocalNow {get{return DateTime.Now};}
    DateTime UtcNow {get{return DateTime.UtcNow };}
    DateTime GpsNow {get{return DateTime.UtcNow - TimeSpan.FronSeconds(LeapSeconds);}}
    void Dispose()
    { //obviously this isn't needed}
}

So the question is, should I impose the IDisposable requirement when I expect most implementations will be using system resources that need to be released? Currently I do just that as it is then easier when the user of the IDateTimeProvider is releasing resources and

if(myDateTimeProvider is IDisposable) ((IDisposable)myDateTimeProvider).Dispose();

would not be needed.

解决方案

This is debatable, but there are examples in the framework that follow this guideline. A good example is Stream - it implements IDisposable even though there are subclasses where this is not necessary.

I would use caution about requiring this of your users, however, unless you truly are fairly certain that nearly all implementations will require IDisposable, and not just a few of them.

这篇关于使用了IDisposable接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:21